简体   繁体   English

在Root设备Android上以编程方式设置动态壁纸

[英]Set Live Wallpaper Programmatically on Rooted Device Android

Is it possible somehow to set Live Wallpaper programmatically using my Application? 有可能以某种方式使用我的应用程序以编程方式设置动态壁纸吗?

I am working on an Application that her purpose is to choose some of the Installed Live Wallpapers on the device and to set it as a Live Wallpaper. 我正在开发一个应用程序,她的目的是选择设备上的一些已安装的动态壁纸,并将其设置为动态壁纸。 This action need to be completed via my Application. 此操作需要通过我的应用程序完成。

As I was researching I found some answers that this can be done with rooting the Android Device? 在我研究的过程中,我找到了一些答案,这可以通过植根Android设备来实现吗?

Can some one help me out how to do that exactly? 有人可以帮我解决这个问题吗?

Android OS prior to Jelly Bean does not allow you to programatically set a live wallpaper. Jelly Bean之前的Android操作系统不允许您以编程方式设置动态壁纸。 For now Jelly Bean supports changing the Live Wallpaper programtically without user interaction 目前,Jelly Bean支持在没有用户交互的情况下以编程方式更改动态壁纸

Sorry to break it to the nay sayers but it is possible to set a live wallpaper programmatically WITHOUT user interaction. 很抱歉打破了不同的说法,但可以通过编程方式设置动态壁纸, 无需用户交互。 It requires: 这个需要:

  1. Your app to be system-privileged 您的应用程序具有系统特权
  2. <uses-permission android:name="android.permission.SET_WALLPAPER_COMPONENT" />
  3. Java reflection (super hacking code) Java反射(超级黑客代码)
  4. A class reference to the desired WallpaperService (live Wallpaper) 所需WallpaperService的类引用(动态壁纸)

NOTE: For item #3, I used my own live wallpaper, MyWallpaperService class 注意:对于第3项,我使用了自己的动态壁纸,MyWallpaperService类

This can only be done if your app is system-privileged and has this permission in the manifest: 只有当您的应用程序具有系统特权且在清单中具有此权限时,才能执行此操作:

<uses-permission android:name="android.permission.SET_WALLPAPER_COMPONENT" />

Now, using reflection, you can call the hidden methods of WallpaperManager to manually set the live wallpaper: 现在,使用反射,您可以调用WallpaperManager的隐藏方法来手动设置动态壁纸:

WallpaperManager manager = WallpaperManager.getInstance(context);
Method method = WallpaperManager.class.getMethod("getIWallpaperManager", null);
Object objIWallpaperManager = method.invoke(manager, null);
Class[] param = new Class[1];
param[0] = ComponentName.class;
method = objIWallpaperManager.getClass().getMethod("setWallpaperComponent", param);

//get the intent of the desired wallpaper service. Note: I created my own
//custom wallpaper service. You'll need a class reference and package
//of the desired live wallpaper 
Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
intent.setClassName(context.getPackageName(), MyWallpaperService.class.getName());

//set the live wallpaper (throws security exception if you're not system-privileged app)
method.invoke(objIWallpaperManager, intent.getComponent());

Refer to the source code: 参考源代码:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM