android动态壁纸调用的简单实例
调用后动态壁纸其实是显示在activity的后面,而activity则是透明显示,这样就可以看到下面的动态壁纸,如果activity不是透明的则什么也看不到。
代码中有用到两个接口
iwallpaperservice mservice;
iwallpaperengine mengine;
我们可以看到该目录下面有三个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服务,所以问题就变成了怎么获取系统权限。