12

私はこれの特定の例をあちこち探していましたが、どこにもオンラインで見つけることができませんでした。

私がやりたいことは、アプリからボタンをクリックして、アプリのライブ壁紙のライブ壁紙プレビューに移動し、ユーザーがそれをアクティブ化することを選択できるようにすることです。

オンラインで読んだ内容は、WallpaperManagerのACTION_CHANGE_LIVE_WALLPAPERと、LiveWallpapersComponentNameを指す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);

投稿するのを忘れた情報がさらに必要な場合は、お知らせください。

*これがAPI16+であることも認識しています。これは、電話がAPI16+の場合の私の場合です。

4

1 に答える 1

20

例も見つかりませんでした。私が最初に気付いたのは、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);

キーは、の2番目のパラメーターにありましたComponentName

技術的には、私の最終的なフォームは、最初に新しいメソッドの階層をサポートし、次に古いメソッド、次にNook Tablet /NookColor固有のインテントが続きます。

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);
        }
    }
}
于 2012-11-04T03:29:54.090 に答える