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

时间:2012-12-03 12:39:27

标签: android live-wallpaper

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

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

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

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:4)

很抱歉打破了不同的说法,但可以通过编程方式设置动态壁纸没有用户互动。它要求:

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

    注意:对于第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());
    

    参考源代码: