Archive

Posts Tagged ‘intent’

Launch your applications on custom secret code

August 13, 2012 8 comments

You might know about some of Android Secret Number or Code, right?

For sample, on the dial pad, type this code: *#*#4636#*#*, the a Testing screen will appear; it’s a secret screen that you do/don’t know about it before.

Dial Pad on SecretCode

Dial Pad on SecretCode

The interesting point here is that you absolutely want to do the same thing, type your custom secret code and your app is launched immediately.

This is what you need to do for it.

1. Create a custom BroadcastReceiver for Secret Code entering; for sample, I named it MySecretCodeReceiver.

This receiver will launch my Activity on detection.

public class MySecretCodeReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
			Intent i = new Intent(context, MainActivity.class);
			i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(i);
		}
	}

}

2. Register the MySecretCodeReceiver properly, like below:

        <receiver android:name=".MySecretCodeReceiver">
    	 	<intent-filter>
          		<action android:name="android.provider.Telephony.SECRET_CODE" />
         		<data android:scheme="android_secret_code" android:host="1711" />
   		</intent-filter>
        </receiver>

You need to make sure that the tag <action> should be like exactly same as above, since it’s the pre-defined action for SECRET_CODE handling. Also, pick a number in attribute “android:host” to your application. For sample here, I pick my lucky number 1711.

That’s pretty much straightforward. Have fun anyway!

Cheers,

Pete Houston

Categories: Tricks & Tips Tags: , , ,

Query all installed Launcher Applications

July 3, 2012 1 comment

The tip for making this happen is that all launchers register these two categories in Manifest

+ CATEGORY_HOME

+ CATEGORY_DEFAULT

and this is my snippet to query ’em all.

	public static ArrayList<ApplicationInfo> getAllLaunchers(Context context) {
		// create new intent
		final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		// all launchers register these two categories
		mainIntent.addCategory(Intent.CATEGORY_HOME);
		mainIntent.addCategory(Intent.CATEGORY_DEFAULT);

		// query normally
		final ArrayList<ResolveInfo> pkgAppsList = (ArrayList<ResolveInfo>)
				context.getPackageManager().queryIntentActivities( mainIntent, 0);

		// you can return 'pkgAppsList'
		// anyway, I need the following information,
		// so I just query what I need
		ArrayList<ApplicationInfo> listAppInfo = new ArrayList<ApplicationInfo>();
		for(ResolveInfo info: pkgAppsList) {
			listAppInfo.add(info.activityInfo.applicationInfo);
		}

		// return
		return listAppInfo;
	}

There you go, gotta do something with it.
Enjoy and have fun!

Cheers,
Pete Houston

Categories: Tricks & Tips Tags: , , , ,

Detect when mobile is booted completely


Some applications are designed to run or do something automatically right after the phone is booted, like services…

This is what I use to run services in my applications after boot

public class BootReceiver extends BroadcastReceiver {
    Context mContext;
    private final String BOOT_ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        String action = intent.getAction();
        if (action.equalsIgnoreCase(BOOT_ACTION)) {
            // do what you need to do here
            ...
        }
    }
}

Cheers,
Pete Houston

Categories: Tricks & Tips Tags: , , ,

Pass complex object structure to Intent

June 24, 2011 1 comment

Intent class supports developers to put several primitive type, however, sometimes we need to pass complex data structure to it. It’s ok to use Serialize, but due to performance, it is kind of not really good.

There is another way around it is to implement data structure to Parcelable interface, in which we need to implement two methods:

	@Override
	public int describeContents() {

	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {

	}

1. describeContents(): describe the kinds of special objects contained in this Parcelable’s marshalled representation.
2. writeToParcel(): flatten this object in to a Parcel.

Also, we need to implement a static field: Parcelable.Creator<T> CREATOR, which will generate the object of class will be implemented.

Check out from Android Developers’ Guide Reference on Parcelable interface

This is a sample:

package pete.android.study;

import android.os.Parcel;
import android.os.Parcelable;

public class SongInfo implements Parcelable {
	private String mTitle;
	private String mSinger;

	public SongInfo(String title, String singer) {
		mTitle = title;
		mSinger = singer;
	}

	public SongInfo(Parcel in) {
		String[] data = new String[2];
		in.readStringArray(data);
		mTitle = data[0];
		mSinger = data[1];
	}

	public void setTitle(String title) {
		mTitle = title;
	}
	public String getTitle() {
		return mTitle;
	}

	public void setSinger(String singer) {
		mSinger = singer;
	}
	public String getSinger() {
		return mSinger;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeStringArray(new String[] {
				mTitle,
				mSinger
		});
	}

	public static final Parcelable.Creator<SongInfo>CREATOR = new Parcelable.Creator<SongInfo>() {

		@Override
		public SongInfo createFromParcel(Parcel source) {
			return new SongInfo(source);
		}

		@Override
		public SongInfo[] newArray(int size) {
			return new SongInfo[size];
		}

	};
}

Now you can put it into an Intent for passing:

data.putExtra("SongInfo", new SongInfo("Happy New Year", "ABBA"));

Or even put an ArrayList

data.putParcelableArrayListExtra("SongList", mListPlaying);

Hope you like it!

Cheers,
Pete Houston