• android动态壁纸调用


    动态壁纸的实现其实就是在Activity中调用动态壁纸服务,通过绑定服务得到IWallpaperService,调用该接口中的attach函数实现壁纸的调用。

    调用后动态壁纸其实是显示在Activity的后面,而Activity则是透明显示,这样就可以看到下面的动态壁纸,如果Activity不是透明的则什么也看不到。


    参考壁纸选择器的源代码:android4.0.3_v1.2\packages\wallpapers\LivePicker

    代码中有用到两个接口

    IWallpaperService mService;

    IWallpaperEngine mEngine;

    这两个接口的源代码参考:android4.0.3_v1.2\frameworks\base\core\java\android\service\wallpaper

    我们可以看到该目录下面有三个aidl接口,分别是

    interface IWallpaperConnection {

        void attachEngine(IWallpaperEngine engine);

        ParcelFileDescriptor setWallpaper(String name);

    }

    oneway interface IWallpaperService {

        void attach(IWallpaperConnection connection,

                IBinder windowToken, int windowType, boolean isPreview,

                int reqWidth, int reqHeight);

    }

    oneway interface IWallpaperEngine {

        void setDesiredSize(int width, int height);

        void setVisibility(boolean visible);

        void dispatchPointer(in MotionEvent event);

        void dispatchWallpaperCommand(String action, int x, int y,            int z, in Bundle extras);

        void destroy();

    }

    定义壁纸管理和壁纸信息变量

    private WallpaperManager mWallpaperManager = null;

    private WallpaperInfo mWallpaperInfo = null;

    private WallpaperConnection mWallpaperConnection = null;

    private Intent mWallpaperIntent;


    初始化这些变量

    mWallpaperManager = WallpaperManager.getInstance(this);

    mWallpaperInfo = mWallpaperManager.getWallpaperInfo();//如果返回null则说明当前不是动态壁纸

    mWallpaperIntent = new Intent(WallpaperService.SERVICE_INTERFACE);

    mWallpaperIntent.setClassName(mWallpaperInfo.getPackageName(), mWallpaperInfo.getServiceName());


    绑定动态壁纸服务

    bindService(mIntent, this, Context.BIND_AUTO_CREATE);

    IWallpaperService mService;//这里有一个adil接口

    在连接监听中试着attach

    public void onServiceConnected(ComponentName name, IBinder service) {

                    mService = IWallpaperService.Stub.asInterface(service);

                    try {

                        mService.attach(this, view.getWindowToken(),

                        //        WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY,
                                WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA,

                                      true, root.getWidth(), root.getHeight());

                    } catch (RemoteException e) {

                        Log.w("", "Failed attaching wallpaper; clearing", e);

                    }

            }


    在bindService的时候发现总是失败,后来发现是权限问题,只有拥有系统权限的apk才可以使用WallpaperService.SERVICE_INTERFACE服务,所以问题就变成了怎么获取系统权限。

  • 相关阅读:
    IsPostBack
    判断客户端.net版本
    js 汉字转换成拼音 转载
    观察者模式
    常用的js阻止冒泡的方法
    jquery中事件的绑定
    uclinux编译 skyeye运行
    dotNet学习之路 Struct与Class异同点
    dotNet学习之路 Delegate内部原理
    设计模式之旅(策略模式) 十号刚发工资的博友们,赶紧跟我一起算算你们的老板有没有给你少发工资。。。
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3073052.html
Copyright © 2020-2023  润新知