Archive for the ‘Learning’ Category

Launch Home Chooser Dialog

After long time digging in Android source code, I’ve found a way to pop-up the Home Chooser dialog.

Home Chooser Dialog

Home Chooser Dialog

Above is how it displays on my phone. Anyway, here the way how to do it:

    static final String HOME_CHOOSER_PACKAGE_NAME = "android";
    static final String HOME_CHOOSER_CLASS_NAME = "";

    private void launchHomeChooser() {
    	Intent i = new Intent(Intent.ACTION_MAIN);

If you search over Internet, there might be many other ways to create a similar Home Chooser dialog, however, it takes lots of coding and customization. Well, I prefer to call the framework one.

One more thing, it works on my device Galaxy SII, running ROM Resurrection Remix v2.5.3 (Android ICS v4.0.4); so I’m not really certain that it would work for every device, but it should work for most of devices then. It runs fine on Android 2.3.x devices, too.

Pete Houston

Categories: Tricks & Tips Tags: , ,

Get date time in custom format

Basically, I use [Date] class to get current time and use [SimpleDateFormat] to format my own output.

Just a sample format:

private String getDateTime() {
    // get date time in custom format
    SimpleDateFormat sdf = new SimpleDateFormat("[yyyy/MM/dd - HH:mm:ss]");
    return sdf.format(new Date(appInfo.firstInstallTime));

Pete Houston

Categories: Tricks & Tips Tags: , ,

Launch Youtube app to play video

July 7, 2012 1 comment

A simple snippet for launching Youtube app to play video.

	// constant value of package & class name of YouTube app
	public static final String YOUTUBE_PACKAGE_NAME = "";
	public static final String YOUTUBE_CLASS_NAME = "";
	 * Launch Youtube to watch an URL
	 * @param context
	 * @param url
	public static void launchYoutube(Context context, String url) {
		Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(url));        
        intent.setClassName(YOUTUBE_PACKAGE_NAME, YOUTUBE_CLASS_NAME);        

Pete Houston

Categories: Tricks & Tips Tags: ,

Create a Service that does a schedule task

July 7, 2012 18 comments

Sometimes you need to do a schedule task for a fixed rate (repeatedly), like [check for emails], [check for battery], [update new Facebook status], etc…

The tip here is to use Service and implement a Timer running a TimerTask for an interval.

My sample here is that I like to get Date and Time in every 10 seconds and display a Toast notification to users.

Here how it looks:

Sample Schedule Task

Sample Schedule Task

So I create my own service called TimeService:

public class TimeService extends Service {
	// constant
	public static final long NOTIFY_INTERVAL = 10 * 1000; // 10 seconds

	// run on another Thread to avoid crash
	private Handler mHandler = new Handler();
	// timer handling
	private Timer mTimer = null;

	public IBinder onBind(Intent intent) {
		return null;

	public void onCreate() {
		// cancel if already existed
		if(mTimer != null) {
		} else {
			// recreate new
			mTimer = new Timer();
		// schedule task
		mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);

	class TimeDisplayTimerTask extends TimerTask {

		public void run() {
			// run on another thread Runnable() {

				public void run() {
					// display toast
					Toast.makeText(getApplicationContext(), getDateTime(),


		private String getDateTime() {
			// get date time in custom format
			SimpleDateFormat sdf = new SimpleDateFormat("[yyyy/MM/dd - HH:mm:ss]");
			return sdf.format(new Date());


It’s easy to understand, isn’t it?

Next is to register the TimerService to AndroidManifest for identification.

        android:theme="@style/AppTheme" >
            android:label="@string/title_activity_main" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

        <service android:name=".TimeService"/>

Our last step is to give a call for starting our service when activity executes.

    public void onCreate(Bundle savedInstanceState) {

        startService(new Intent(this, TimeService.class));

Finally, just run on the phone and see the result.
The implementation is pretty straightforward and simple. You can freely use this way to create some schedule tasks for your applications.

Pete Houston

Categories: Tutorials Tags: , ,

Error creating project in ADT rev.20

July 6, 2012 2 comments

Now this is what happen after updating ADT to rev.20.

Error create project in ADT rev.20

Error create project in ADT rev.20


The problem is that the ADT looks for the Android Support Library in <ANDROID-SDK>\extras\android\support , but in facts, it stays in <ANDROID-SDK>\extras\android\compatibility

So the solution is to copy the folder [\compatibility] and rename it to [\support] then it will be fixed.



Pete Houston


Categories: Tricks & Tips Tags: ,

Do TaskManager apps really kill other apps immediately?

It’s the question that I was wondering for while “How to kill other running processes?”.

I’ve found some methods to proceed this action.

1. Use Process to kill : android.os.Process.killProcess()

This is what it is said in documentation:

Kill the process with the given PID. Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes.

So basically, it means that it cannot kill other processes unless they share the same UID.

> Epic Failed!


2. Use Process to send SIGNAL: android.os.Process.sendSignal()

Look at this page on signal and kill

So it means the TaskKiller-like apps should have privileges or appropriate permission on other process in order to kill, otherwise, it must depend on system to decide when to kill.

> Epic Failed!

3. Use ActivityManger to kill:

Have the system immediately kill all background processes associated with the given package. This is the same as the kernel killing those processes to reclaim memory; the system will take care of restarting these processes in the future as needed.

Above is what the docs say about it.

However, does it really kill the give package immediately? I’ve tried but fail. It really depends on system to kill.

> Epic Failed! Or Not?


4. Use ActivityManager to kill:

– It’s deprecated now, nevermind about this!


So if TaskKiller-like apps cannot kill other running processes immediately, then what does it do because users might feel like it really works right away?

– One thing might be that the TaskKiller-like apps send the signal or whatever killing like above, then displaying on screen list of running process not in SUSPENDED state, just hide from users. It could be an answer, there might be others as well but I cannot think of.

There are some references on StackOverflow talking about this matter:

How do Task Manager’s kill apps?

How to kill currently running task in android?

Android: kill processes and close applications

Android process killer

Feel free to share if you got any idea.



Pete Houston


Categories: Tricks & Tips Tags: , , ,

New Cache for Better Memory Performance

Hanging around and reading Android articles, and suddenly I want to check out new information on Android Training Page.

As a developer having lots of interest in Android Performance & Optimization, I check this section first: Cache with Bitmaps.

Got a note from the page:

Note: In the past, a popular memory cache implementation was a SoftReference or WeakReference bitmap cache, however this is not recommended. Starting from Android 2.3 (API Level 9) the garbage collector is more aggressive with collecting soft/weak references which makes them fairly ineffective. In addition, prior to Android 3.0 (API Level 11), the backing data of a bitmap was stored in native memory which is not released in a predictable manner, potentially causing an application to briefly exceed its memory limits and crash.

Just like something light up in my head, this explains why sometimes some of my apps crash in memory limitation strangely before.

The two new class introduced to work around with Bitmap Caching is: LruCache and DiskLruCache.

So if you got any problem with bitmap memory, this could be a hint to your optimizing solution for your Android apps.



Pete Houston

Categories: Tutorials Tags: , ,