5

是否有可能以某种方式使用我的应用程序以编程方式设置动态壁纸?

我正在开发一个应用程序,她的目的是在设备上选择一些已安装的动态壁纸并将其设置为动态壁纸。此操作需要通过我的应用程序完成。

在我研究的过程中,我找到了一些答案,这可以通过植根 Android 设备来完成?

有人可以帮我弄清楚如何做到这一点吗?

4

2 回答 2

5

Jelly Bean 之前的 Android 操作系统不允许您以编程方式设置动态壁纸。目前,Jelly Bean 支持以编程方式更改动态壁纸,无需用户交互

于 2012-12-06T12:08:00.983 回答
4

很抱歉向反对者打破它,但可以在没有用户交互的情况下以编程方式设置动态壁纸。这个需要:

  1. 您的应用程序具有系统特权
  2. <uses-permission android:name="android.permission.SET_WALLPAPER_COMPONENT" />
  3. Java反射(超级黑客代码)
  4. 对所需WallpaperService(动态壁纸)的类引用

注意:对于第 3 项,我使用了自己的动态壁纸,MyWallpaperService 类

仅当您的应用程序具有系统特权并且在清单中具有此权限时,才能执行此操作:

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

现在,使用反射,您可以调用 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());

参考源代码:

于 2015-09-17T18:08:35.497 回答