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

标签 android live-wallpaper

我一直在寻找这方面的具体示例,但在网上找不到任何地方。

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

根据我在网上阅读的内容,我要使用 WallpaperManager's ACTION_CHANGE_LIVE_WALLPAPER 和 EXTRA_LIVE_WALLPAPER_COMPONENT 指向我的 LiveWallpapers ComponentName。

这是我目前所拥有的代码。有人知道我做错了什么吗?截至目前,我点击了按钮,但没有任何反应......(我记录了它,它实际上到达了这段代码)。

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+ 时的情况

最佳答案

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

对上面的 block 进行一些调试,这是我的最终形式......

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 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);
        }
    }
}

关于android - 如何从应用程序移动到动态壁纸预览?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12842924/

相关文章:

android - 是否可以在activityview中调用webview onclicking imageview?

android - Paypal for android 如何在回调中识别付款项?

android - 如何更改 M2E 本地存储库位置?

java - Android 在动态壁纸中播放电影文件

android - 动态壁纸预览缩略图大小

Intent 不为空时的 Android "Failure delivering result ResultInfo"

android - 由于某些中间构造函数调用,没有可用的 TYPE 封闭实例

android - 从横向到纵向的屏幕方向不起作用

java - Android:从 url 查看视频流

java - 使用一系列图像的动态壁纸