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

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

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

现在我已经在线阅读了,我将使用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不需要一个String,而是一个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,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);
        }
    }
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...