我無法要麼找到一個例子。我注意到的第一件事情是EXTRA_LIVE_WALLPAPER_COMPONENT
不需要字符串,而是ComponentName
。我的第一個切與ComponentName
是這樣的:
ComponentName component = new ComponentName(getPackageName(), "LiveWallpaperService");
intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, component);
startActivityForResult(intent, REQUEST_SET_LIVE_WALLPAPER);
這並沒有削減它,所以我挖成Android源代碼,發現LiveWallpaperChange.java
如下:
Intent queryIntent = new Intent(WallpaperService.SERVICE_INTERFACE);
queryIntent.setPackage(comp.getPackageName());
List<ResolveInfo> list = getPackageManager().queryIntentServices(queryIntent, PackageManager.GET_META_DATA);
一點點調試與以上大塊,這是我的最終形式...
ComponentName component = new ComponentName(getPackageName(), getPackageName() + ".LiveWallpaperService");
intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, component);
startActivityForResult(intent, REQUEST_SET_LIVE_WALLPAPER);
關鍵是在ComponentName
的第二個參數。
從技術上講,我的最終形式支持新的方法第一的層次結構,其次是舊的,其次的Nook平板電腦/ Nook Color的特定意圖:
Intent intent;
// try the new Jelly Bean direct android wallpaper chooser first
try {
ComponentName component = new ComponentName(getPackageName(), getPackageName() + ".LiveWallpaperService");
intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, component);
startActivityForResult(intent, REQUEST_SET_LIVE_WALLPAPER);
}
catch (android.content.ActivityNotFoundException e3) {
// try the generic android wallpaper chooser next
try {
intent = new Intent(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
startActivityForResult(intent, REQUEST_SET_LIVE_WALLPAPER);
}
catch (android.content.ActivityNotFoundException e2) {
// that failed, let's try the nook intent
try {
intent = new Intent();
intent.setAction("com.bn.nook.CHANGE_WALLPAPER");
startActivity(intent);
}
catch (android.content.ActivityNotFoundException e) {
// everything failed, let's notify the user
showDialog(DIALOG_NO_WALLPAPER_PICKER);
}
}
}
啊,謝謝。因爲它只有API 16+,所以我放棄實現這個想法,但最有可能在以後使用此代碼。感謝您的描述性答案! –
我得到一個錯誤 12-06 14:18:26.936:W/CHANGE_LIVE_WALLPAPER(11898):不是一個動態壁紙:ComponentInfo {com.android.noisefield/com.android.noisefield.LiveWallpaperService} 可以請你如果我不是那個人的主人,告訴我如何更換壁紙?而不是getPackageName()將包放置爲字符串? 例如String packageName =「com.android.noisefield」; 如何用getPackageName()替換packageName? 謝謝先生。 – Naskov