我也找不到一个例子.我注意到的第一件事是EXTRA_LIVE_WALLPAPER_COMPONENT不需要一个String,而是一个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 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);
}
}
}