我也找不到例子。我注意到的第一件事是,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 Tablet/Nook Color特定的意图层次结构:
Intent intent;
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 {
intent = new Intent(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
startActivityForResult(intent, REQUEST_SET_LIVE_WALLPAPER);
}
catch (android.content.ActivityNotFoundException e2) {
try {
intent = new Intent();
intent.setAction("com.bn.nook.CHANGE_WALLPAPER");
startActivity(intent);
}
catch (android.content.ActivityNotFoundException e) {
showDialog(DIALOG_NO_WALLPAPER_PICKER);
}
}
}