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:
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!
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)
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
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!
Sorry, but I forgot one question.
What if I just have a package name?
Is there any way to know the class(es)?
Thanks,
If you just know the package name then you could use PackageManager and use getPackageInfo :)
Wow, thank you for the additional info!
I will definitely try this.
Best regards,
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
It is really great! Thanks so much!
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
do something like
intent.putExtra("DetailPageArgs", args);
and remove the
intent.putExtras(extra);
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.
The exact thing what i was searching for. Thanks lot
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
Great, thanks Almond, just what I was after - nice and concise and a good example used.
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
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.
Post a Comment