android – 如何从应用程序转移到动态壁纸预览?

前端之家收集整理的这篇文章主要介绍了android – 如何从应用程序转移到动态壁纸预览?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在寻找一个具体的例子,无法在任何地方找到它.

我想要做的是:从我的应用程序点击一个按钮,并移动到我的应用程序动态壁纸的动态壁纸预览,所以用户可以选择激活它.

现在我已经在线阅读了,我将使用WallpaperManager’s ACTION_CHANGE_LIVE_WALLPAPER与EXTRA_LIVE_WALLPAPER_COMPONENT指向我的LiveWallpapers ComponentName.

这是我迄今为止的代码.有谁知道我在做错什么?到目前为止,我点击按钮,没有任何反应…(我记录它实际上达到这个代码).

  1. Intent i = new Intent();
  2. i.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
  3. i.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,"com.example.myapp.livewallpaper.LiveWallpaperService");
  4. startActivity(i);

如果您需要任何我忘记发布的信息让我知道.

*我也知道这是API 16,这只是我的情况,当手机是API 16

解决方法

我也找不到一个例子.我注意到的第一件事是EXTRA_LIVE_WALLPAPER_COMPONENT不需要一个String,而是一个ComponentName.我使用ComponentName的第一个剪辑如下所示:
  1. ComponentName component = new ComponentName(getPackageName(),"LiveWallpaperService");
  2. intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
  3. intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,component);
  4. startActivityForResult(intent,REQUEST_SET_LIVE_WALLPAPER);

那没有削减它,所以我挖掘了Android源代码,并在LiveWallpaperChange.java中找到以下内容

  1. Intent queryIntent = new Intent(WallpaperService.SERVICE_INTERFACE);
  2. queryIntent.setPackage(comp.getPackageName());
  3. List<ResolveInfo> list = getPackageManager().queryIntentServices( queryIntent,PackageManager.GET_Meta_DATA);

用上面的一点调试一下,这是我的最终形式…

  1. ComponentName component = new ComponentName(getPackageName(),getPackageName() + ".LiveWallpaperService");
  2. intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
  3. intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,REQUEST_SET_LIVE_WALLPAPER);

关键是ComponentName的第二个参数.

在技​​术上,我的最终形式首先支持方法的层次结构,其次是旧的,其次是Nook Tablet / Nook Color的具体意图:

  1. Intent intent;
  2.  
  3. // try the new Jelly Bean direct android wallpaper chooser first
  4. try {
  5. ComponentName component = new ComponentName(getPackageName(),getPackageName() + ".LiveWallpaperService");
  6. intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
  7. intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,component);
  8. startActivityForResult(intent,REQUEST_SET_LIVE_WALLPAPER);
  9. }
  10. catch (android.content.ActivityNotFoundException e3) {
  11. // try the generic android wallpaper chooser next
  12. try {
  13. intent = new Intent(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
  14. startActivityForResult(intent,REQUEST_SET_LIVE_WALLPAPER);
  15. }
  16. catch (android.content.ActivityNotFoundException e2) {
  17. // that Failed,let's try the nook intent
  18. try {
  19. intent = new Intent();
  20. intent.setAction("com.bn.nook.CHANGE_WALLPAPER");
  21. startActivity(intent);
  22. }
  23. catch (android.content.ActivityNotFoundException e) {
  24. // everything Failed,let's notify the user
  25. showDialog(DIALOG_NO_WALLPAPER_PICKER);
  26. }
  27. }
  28. }

猜你在找的Android相关文章