微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/android/312329.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐