【问题标题】:How do I move to Live Wallpaper preview from app?如何从应用程序移动到动态壁纸预览?
【发布时间】:2012-10-11 15:15:04
【问题描述】:

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

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

现在我在网上阅读的内容,我将使用WallpaperManager's 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+ 的情况

【问题讨论】:

    标签: android live-wallpaper


    【解决方案1】:

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

    【讨论】:

    • 啊,谢谢。我放弃了实现这个想法,因为它只是 API 16+,但很可能在以后使用这个代码。感谢您的描述性回答!
    • 我收到一个错误 12-06 14:18:26.936:W/CHANGE_LIVE_WALLPAPER(11898):不是动态壁纸:ComponentInfo{com.android.noisefield/com.android.noisefield.LiveWallpaperService}如果我不是那张壁纸的所有者,你能告诉我如何更换壁纸吗?而不是 getPackageName() 将包作为字符串放置?例如 String packageName = "com.android.noisefield";如何用 getPackageName() 替换 packageName?谢谢先生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多