Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch an Activity from another Application in Android

I want to launch an installed package from my Android application. I assume that it is possible using intents, but I didn't find a way of doing it. Is there a link, where to find the information?

like image 772
Bastian Avatar asked Oct 06 '10 11:10

Bastian


People also ask

How do I start an activity from another application?

To allow other apps to start your activity in this way, you need to add an <intent-filter> element in your manifest file for the corresponding <activity> element.

How do I launch an Android app from another app?

This example demonstrate about How to Launch an application from another application on Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Which method is used to launch another activity Android?

In order to launch one activity from another, you must use an Intent object. After instantiating a new Intent, you simply call the startActivity() method, passing the intent as an argument.

Can an app launch another app?

Using intents even allows your app to start an activity that is contained in a separate app. An Intent can be explicit in order to start a specific component (a specific Activity instance) or implicit in order to start any component that can handle the intended action (such as "capture a photo").


2 Answers

If you don't know the main activity, then the package name can be used to launch the application.

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address"); if (launchIntent != null) {      startActivity(launchIntent);//null pointer check in case package name was not found } 
like image 199
andep Avatar answered Oct 07 '22 18:10

andep


I know this has been answered but here is how I implemented something similar:

Intent intent = getPackageManager().getLaunchIntentForPackage("com.package.name"); if (intent != null) {     // We found the activity now start the activity     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     startActivity(intent); } else {     // Bring user to the market or let them choose an app?     intent = new Intent(Intent.ACTION_VIEW);     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     intent.setData(Uri.parse("market://details?id=" + "com.package.name"));     startActivity(intent); } 

Even better, here is the method:

public void startNewActivity(Context context, String packageName) {     Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);     if (intent != null) {         // We found the activity now start the activity         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);         context.startActivity(intent);     } else {         // Bring user to the market or let them choose an app?         intent = new Intent(Intent.ACTION_VIEW);         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);         intent.setData(Uri.parse("market://details?id=" + packageName));         context.startActivity(intent);     } } 

Removed duplicate code:

public void startNewActivity(Context context, String packageName) {     Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);     if (intent == null) {         // Bring user to the market or let them choose an app?         intent = new Intent(Intent.ACTION_VIEW);         intent.setData(Uri.parse("market://details?id=" + packageName));     }     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     context.startActivity(intent); } 
like image 44
Jared Burrows Avatar answered Oct 07 '22 17:10

Jared Burrows