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

Bra*_*ano 12 android live-wallpaper

我一直在寻找一个具体的例子,无法在任何地方找到它.

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

现在我在网上看到的,我将使用WallpaperManager的 ACTION_CHANGE_LIVE_WALLPAPER和指向我的LiveWallpapers ComponentName的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);
Run Code Online (Sandbox Code Playgroud)

如果您需要我忘记发布的更多信息,请告诉我.

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

Ric*_*ard 20

我也找不到一个例子.我注意到的第一件事是它EXTRA_LIVE_WALLPAPER_COMPONENT不需要String,而是a 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);
Run Code Online (Sandbox Code Playgroud)

这没有削减它,所以我挖掘了Android源代码,发现以下内容LiveWallpaperChange.java:

Intent queryIntent = new Intent(WallpaperService.SERVICE_INTERFACE);
queryIntent.setPackage(comp.getPackageName());
List<ResolveInfo> list = getPackageManager().queryIntentServices( queryIntent, PackageManager.GET_META_DATA);
Run Code Online (Sandbox Code Playgroud)

用上面的块进行一点调试,这是我的最终形式......

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);
Run Code Online (Sandbox Code Playgroud)

关键是在第二个参数中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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我收到错误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?谢谢先生. (5认同)