Amazon Gift Cards

Thursday, October 8, 2009

Launching other application using code in Android

Previously we blogged about how to delete application by their package name. Now here is how to open applications using code, note this is not the same as intent-filter.

Say you want to open fuelgauge in Android 1.6, to do this.
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.fuelgauge.PowerUsageSummary");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity( intent);


Explanation
To open other people's application, you need to make sure that in their manifest file, the author specify the class to have the android.intent.action.MAIN intent-filter added to them.
final Intent intent = new Intent(Intent.ACTION_MAIN, null);

We then add category that this new intent will be launching something
intent.addCategory(Intent.CATEGORY_LAUNCHER);

Then we get identify the application we need to open by using ComponentName, here you specify the package name of the application as first argument and the class we want to open as the second one. You must understand that com.android.settings has a lot of classes that have Main intent-filter under it making the second argument to be the specific class that we need. (this is more than one line)
final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.fuelgauge.PowerUsageSummary");

After we identify the component we want, we set it to our intent
intent.setComponent(cn);

We then tell the intent that open opening this one make it as a new task
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Then finally start our intent
startActivity( intent);

Resources
LauncherProvider.java
IRC #android-dev room

Update History
   Jan 17, 2012 - Visual Update

17 comments:

Frederyck said...

Hi! Thank u first for ur tutorials, they going to help me a lot to start with Android.
By reading this one a question comes to me.
How could i re-use the activity in the home screen that lists applications with a slide effect when draging it with fingers ?

Thx!

admin said...

Thanks, glad its helping you :) By reuse you mean using the one from google? I think its better if you get the idea on how the staff is being done first before reusing any codes, btw most of the sources on launching staff (like applications, widget, etc) could be found on the official Launcher.git (http://android.git.kernel.org/?p=platform/packages/apps/Launcher.git;a=tree;f=src/com/android/launcher;h=86727674d25930f667f644a76c2478a35e13b0f2;hb=154f4a3dcc3ebb07fa7db51d3ff57a1557990cc0)

Frederyck said...

Thank you for your answer.
Well by reuse i mean using in part the one from Google as a view component that could be binded with a object list...
In the same idea i'm interested of use android live folders..like a real folder where i could drag items but it seems like not easy to do.
Anyway! i'm going first to get Android source code and try to get how they do it

KF0310 said...

Hello,
Thank you for this valuable info.
So, in order to open other people’s application,
we need to know both the package and class names.
Correct? I would like to confirm it.
Thank you again!

KF0310 said...

Sorry, but I forgot one question.
What if I just have a package name?
Is there any way to know the class(es)?
Thanks,

admin said...

If you just know the package name then you could use PackageManager and use getPackageInfo :)

KF0310 said...

Wow, thank you for the additional info!
I will definitely try this.
Best regards,

Shubham said...

Hi friends,
Nice Tutorial...thanx lot.
I have a quest ,how can i hide a particular install application name from install application name list.

Thanks in Advnace

Anonymous said...

It is really great! Thanks so much!

Anonymous said...

thanks for your tutorial. It helps me.

Now I have a question. If I want to pass some data from my activity to another activity in another application. How can I do that? I tried to use intent and put the bundle to intent. But the activity in another application got no bundle. here is my codes:

--- my activity ---
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
Bundle extra = new Bundle();
String []args = {"3819412104","It's Supernatural with Sid Roth", "2", "KDTN-DT", "1311962400000", "1311964200000", "134"};
extra.putStringArray("DetailPageArgs", args);
intent.putExtras(extra);
intent.setComponent(new ComponentName("com.example.test", "com.example.test.ddd.DetailPageActivity"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);


---- DetailActivity -----
Intent intent = getIntent();
Bundle args = intent.getExtras();

args is null.
Could you let me know what's going on. What can I do to get the passed data?
thanks a lot

Almond said...

do something like
intent.putExtra("DetailPageArgs", args);
and remove the
intent.putExtras(extra);

Anonymous said...

Hi Almond,

Thank you so much for your help. I just try and it works. I am able to get the arguments.

thanks for your big help.

Anonymous said...

The exact thing what i was searching for. Thanks lot

pankzy said...

hey really a nice help provided by you. really appreciate that.
I am also having a question. I m new to android apps development and I want to launch an application when a user draws a gesture on homescreen.That means user doesnt need to use any application but the homescreen should be able to detect a gesture drawn and a service sort of thing should get triggerred to launch an application.
If anyone could help it will be great.
Thanks in advance

Anonymous said...

Great, thanks Almond, just what I was after - nice and concise and a good example used.

Anonymous said...

Hi!
I learnt a lot thanks to you tutorial. Thank you for it.
I launch APK2 with APK1, APK1 is still running in background. How I can come back to APK1? with the code which is running in APK1.
I tried in 2 differents ways :
*use the onRestart() methode, onStart ...
*use the intent created an close it: this.stopService(i); i.setAction(i.ACTION_DELETE); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
But nothing work... Do you have an idea how to do this?
BR,
Mick

Praneeth Avula said...

Hi,
Thanks for the nice tutorials.

This may not be the correct place to ask the question, but this is related to opening the application.
With the widget onclick I want to open the current running activity of the same package.
My app widget click should work as same as the application shortcut click.
Please suggest.

Thanks.