如何从应用程序移动到实时壁纸预览?

12

我一直在寻找一个特定的例子,但在网上找不到。

我想做的是:从我的应用程序中点击一个按钮,然后转到我的应用程序的实时壁纸预览,以便用户可以选择激活它。

根据我在线上阅读的资料,我需要使用WallpaperManager的ACTION_CHANGE_LIVE_WALLPAPER和指向我的LiveWallpapers组件名称的EXTRA_LIVE_WALLPAPER_COMPONENT。

这是我迄今为止所拥有的代码。 有人知道我做错了什么吗?现在当我点击按钮时什么都没有发生...(我记录了日志并且实际上已经达到了这段代码)。

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

如果我遗漏了任何需要提供的信息,请让我知道。

我也知道这是API 16+,这只是对于当手机是API 16+的情况而言。

1个回答

20

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

1
啊,谢谢。我放弃了实现这个想法,因为它只适用于API 16+,但很可能在以后的某个时候会使用这段代码。感谢您详细的回答! - Brandon Romano
5
我遇到了一个错误 12-06 14:18:26.936: W/CHANGE_LIVE_WALLPAPER(11898): Not a live wallpaper: ComponentInfo{com.android.noisefield/com.android.noisefield.LiveWallpaperService}请问如果我不是壁纸的所有者,该如何更改壁纸?可以用 getPackageName() 替换包名字符串吗?例如:String packageName = "com.android.noisefield";如何使用 getPackageName() 替换 packageName?谢谢。 - Naskov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接