Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

android - How do I move to Live Wallpaper preview from app?

I've been looking all over for a specific example of this and couldn't find it online anywhere.

What I want to do is: From my app click a button and move to the Live Wallpaper preview of my apps live wallpaper, so the user can choose to activate it.

Now of what I've read online, I'm to use WallpaperManager's ACTION_CHANGE_LIVE_WALLPAPER with EXTRA_LIVE_WALLPAPER_COMPONENT pointing to my LiveWallpapers ComponentName.

Here's my code of what I have so far. Anybody know what I'm doing wrong? As of now I click the button and nothing happens... (I logged it and it's actually reaching this code).

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

If you need any more info that I forgot to post let me know.

*I am also aware this is API 16+, this is just my case for when the phone is API 16+

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I couldn't find an example either. The first thing I noticed was that the EXTRA_LIVE_WALLPAPER_COMPONENT doesn't require a String, but a ComponentName. My first cut with ComponentName looked like this:

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

That didn't cut it, so I dug into the Android source code and found the following in LiveWallpaperChange.java:

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

A little debugging with the above chunk, and this is my final form...

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

The key was in the second parameter to ComponentName.

Technically, my final form supports a hierarchy of the new method first, followed by the old, followed by the Nook Tablet/Nook Color specific intent:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...