簡體   English   中英

在Root設備Android上以編程方式設置動態壁紙

[英]Set Live Wallpaper Programmatically on Rooted Device Android

有可能以某種方式使用我的應用程序以編程方式設置動態壁紙嗎?

我正在開發一個應用程序,她的目的是選擇設備上的一些已安裝的動態壁紙,並將其設置為動態壁紙。 此操作需要通過我的應用程序完成。

在我研究的過程中,我找到了一些答案,這可以通過植根Android設備來實現嗎?

有人可以幫我解決這個問題嗎?

Jelly Bean之前的Android操作系統不允許您以編程方式設置動態壁紙。 目前,Jelly Bean支持在沒有用戶交互的情況下以編程方式更改動態壁紙

很抱歉打破了不同的說法,但可以通過編程方式設置動態壁紙, 無需用戶交互。 這個需要:

  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());

參考源代碼:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM