我也找不到例子。我注意到的第一件事是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 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);
}
}
}