Quantcast
Channel: Yougli's Box
Viewing all articles
Browse latest Browse all 3

Live Wallpaper – Binding an Activity to the ‘Open’ button of the market

$
0
0

Introduction

When Android 2.1 was released, it came out with this nice new feature called ‘Live Wallpapers‘. I’m not going to describe in details what Live Wallpapers are here, but what people didn’t realize at that time is that they can’t be considered as traditional applications or widgets:

  • They don’t appear in the application launcher, instead you need to go through numerous steps to open the Live Wallpaper Preview and set your wallpaper
  • The market’s ‘Open’ button is disabled by default

In other words, if you’re not used to Live Wallpapers, you may feel a bit lost trying to use one. When I launched Shake Them All, I received a lot of feedback asking how to launch the application, some of them thought it just didn’t work, probably because they couldn’t launch it through regular ways.

My opinion is that the guys at Google should enable the ‘Open’ button of the market by default, and bind it to the Live Wallpaper Preview set on the installed Wallpaper. In the meantime, the only solution I found for Shake Them All was to bind my own Activity to the ‘Open’ button, and use an Intent action to have it launch the Live Wallpaper Picker while displaying a Toast message.

Market

The 'Open' button is enabled...

Live Wallpaper Picker

... and launches the Live Wallpaper Picker with a custom message

Binding an Activity to the ‘Open’ button of the market

First, you will need a simple Activity:

package net.yougli.shakethemall;

import android.app.Activity;
import android.os.Bundle;

public class OpenActivity extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
    }
}

Now, binding this Activity to the ‘Open’ button of the market is simply done by adding these lines to your AndroidManifest.xml, inside the <application> element:

<activity
	android:label="Home"
	android:name=".OpenActivity"
	android:exported="true">
	<intent-filter>
		<action android:name="android.intent.action.MAIN" />
		<category android:name="android.intent.category.INFO" />
	</intent-filter>
</activity>

Basically, this declares our new Activity, and set the Intent filter that will bind it to our ‘Open’ button.

Launching the Live Wallpaper Picker

Now that our ‘Open’ button can do something, let it launch the Live Wallpaper Picker:

package net.yougli.shakethemall;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.os.Bundle;

public class OpenActivity extends Activity {
	private int REQUEST_CODE = 1;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Intent intent = new Intent();
        intent.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
    	super.onActivityResult(requestCode, resultCode, intent);
    	if (requestCode == REQUEST_CODE)
    		finish();
    }
}

To achieve this we create a new Intent, set its action to ACTION_LIVE_WALLPAPER_CHOOSER, and launch it with startActivityForResult().

I could have simply used startActivity(), but I wanted to make sure my Activity is closed as soon as the user selects a wallpaper. Hence the override of the onActivityResult() method and its call of finish().

Displaying a Toast

Ok now, that’s nice, the user will see the list of Live Wallpapers installed on its system, but he still has to select one before really using it, so we’re going to help him a little by suggesting him what to do with the help of a Toast message:

package net.yougli.shakethemall;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class OpenActivity extends Activity {
	private int REQUEST_CODE = 1;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Toast toast = Toast.makeText(this, &quot;Choose \&quot;Shake Them All!\&quot; in the list to start the Live Wallpaper.&quot;, Toast.LENGTH_LONG);
		toast.show();

        Intent intent = new Intent();
        intent.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
    	super.onActivityResult(requestCode, resultCode, intent);
    	if (requestCode == REQUEST_CODE)
    		finish();
    }
}

To display a Toast, we simply create one through the static makeText() method, which takes the text to display, and the display time as parameters. We then call the show() method to display it. Of course, the Toast needs to be displayed before we launch the picker Activity.

Note that you could, for instance, make your Toast a little more attractive by creating a custom View to be displayed in it, via the setView() method.

Voilà, that’s all.

Again, this is not the perfect solution, but hopefully your users will fell a bit less disappointed now … while Google make some changes in the Market :)


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images