Open another application from your own (intent)

AndroidAndroid Intent

Android Problem Overview


I know how to update my own programs, and I know how to open programs using the a predefined Uri (for sms or email for example)

I need to know how I can create an Intent to open MyTracks or any other application that I don't know what intents they listen to.

I got this info from DDMS, but I havn't been succesful in turning this to an Intent I can use. This is taken from when opening MyTracks manually.

Thanks for your help

05-06 11:22:24.945: INFO/ActivityManager(76): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.google.android.maps.mytracks/com.google.android.apps.mytracks.MyTracks bnds=[243,338][317,417] }
05-06 11:22:25.005: INFO/ActivityManager(76): Start proc com.google.android.maps.mytracks for activity com.google.android.maps.mytracks/com.google.android.apps.mytracks.MyTracks: pid=1176 uid=10063 gids={3003, 1015}
05-06 11:22:26.995: INFO/ActivityManager(76): Displayed activity com.google.android.maps.mytracks/com.google.android.apps.mytracks.MyTracks: 1996 ms (total 1996 ms)

Android Solutions


Solution 1 - Android

I have work it like this,

/** Open another app.
 * @param context current Context, like Activity, App, or Service
 * @param packageName the full package name of the app to open
 * @return true if likely successful, false if unsuccessful
 */
public static boolean openApp(Context context, String packageName) {
    PackageManager manager = context.getPackageManager();
    try {
        Intent i = manager.getLaunchIntentForPackage(packageName);
        if (i == null) {
            return false;
            //throw new ActivityNotFoundException();
        }
        i.addCategory(Intent.CATEGORY_LAUNCHER);
        context.startActivity(i);
        return true;
    } catch (ActivityNotFoundException e) {
        return false;
    }
}

Example usage:

openApp(this, "com.google.android.maps.mytracks");

Solution 2 - Android

Firstly, the concept of "application" in Android is slightly an extended one.

An application - technically a process - can have multiple activities, services, content providers and/or broadcast listeners. If at least one of them is running, the application is up and running (the process).

So, what you have to identify is how do you want to "start the application".

Ok... here's what you can try out:

  1. Create an intent with action=MAIN and category=LAUNCHER
  2. Get the PackageManager from the current context using context.getPackageManager
  3. packageManager.queryIntentActivity(<intent>, 0) where intent has category=LAUNCHER, action=MAIN or packageManager.resolveActivity(<intent>, 0) to get the first activity with main/launcher
  4. Get the ActivityInfo you're interested in
  5. From the ActivityInfo, get the packageName and name
  6. Finally, create another intent with with category=LAUNCHER, action=MAIN, componentName = new ComponentName(packageName, name) and setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
  7. Finally, context.startActivity(newIntent)

Solution 3 - Android

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(ComponentName.unflattenFromString("com.google.android.maps.mytracks/com.google.android.apps.mytracks.MyTracks"));
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(intent);

EDIT :

as suggested in comments, add one line before startActivity(intent);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Solution 4 - Android

If you already have the package name you wish to activate, you can use the following code which is a bit more generic:

PackageManager pm = context.getPackageManager();
Intent appStartIntent = pm.getLaunchIntentForPackage(appPackageName);
if (null != appStartIntent)
{
	context.startActivity(appStartIntent);
}

I found that it works better for cases where the main activity was not found by the regular method of start the MAIN activity.

Solution 5 - Android

This is the code of my solution base on MasterGaurav solution:

private void  launchComponent(String packageName, String name){
	Intent launch_intent = new Intent("android.intent.action.MAIN");
	launch_intent.addCategory("android.intent.category.LAUNCHER");
	launch_intent.setComponent(new ComponentName(packageName, name));
	launch_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	
	activity.startActivity(launch_intent);
}

public void startApplication(String application_name){
	try{
		Intent intent = new Intent("android.intent.action.MAIN");
		intent.addCategory("android.intent.category.LAUNCHER");
		
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		List<ResolveInfo> resolveinfo_list = activity.getPackageManager().queryIntentActivities(intent, 0);
		
		for(ResolveInfo info:resolveinfo_list){
			if(info.activityInfo.packageName.equalsIgnoreCase(application_name)){
				launchComponent(info.activityInfo.packageName, info.activityInfo.name);
				break;
			}
		}
	}
	catch (ActivityNotFoundException e) {
		Toast.makeText(activity.getApplicationContext(), "There was a problem loading the application: "+application_name,Toast.LENGTH_SHORT).show();
	}
}

Solution 6 - Android

Using the solution from inversus, I expanded the snippet with a function, that will be called when the desired application is not installed at the moment. So it works like: Run application by package name. If not found, open Android market - Google play for this package.

public void startApplication(String packageName)
{
    try
    {
        Intent intent = new Intent("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.LAUNCHER");

        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        List<ResolveInfo> resolveInfoList = getPackageManager().queryIntentActivities(intent, 0);
        
        for(ResolveInfo info : resolveInfoList)
            if(info.activityInfo.packageName.equalsIgnoreCase(packageName))
            {
                launchComponent(info.activityInfo.packageName, info.activityInfo.name);
                return;
            }
        
        // No match, so application is not installed
        showInMarket(packageName);
    }
    catch (Exception e) 
    {
        showInMarket(packageName);
    }
}

private void launchComponent(String packageName, String name)
{
    Intent intent = new Intent("android.intent.action.MAIN");
    intent.addCategory("android.intent.category.LAUNCHER");
    intent.setComponent(new ComponentName(packageName, name));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    startActivity(intent);
}

private void showInMarket(String packageName)
{
	Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName));
	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	startActivity(intent);
}

And it is used like this:

startApplication("org.teepee.bazant");

Solution 7 - Android

Use this :

    PackageManager pm = getPackageManager();
	Intent intent = pm.getLaunchIntentForPackage("com.package.name");
	startActivity(intent);

Solution 8 - Android

Open application if it is exist, or open Play Store application for install it:

private void open() {
    openApplication(getActivity(), "com.app.package.here");
}

public void openApplication(Context context, String packageN) {
    Intent i = context.getPackageManager().getLaunchIntentForPackage(packageN);
    if (i != null) {
        i.addCategory(Intent.CATEGORY_LAUNCHER);
        context.startActivity(i);
    } else {
        try {
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageN)));
        }
        catch (android.content.ActivityNotFoundException anfe) {
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + packageN)));
        }
    }
}

Solution 9 - Android

To Start another application activity from my application Activity. It is working fine for me.

Below code will work if the another application already installed in your phone otherwise it is not possible to redirect form one app to another app.So make sure your app launched or not

Intent intent = new Intent();
intent.setClassName("com.xyz.myapplication", "com.xyz.myapplication.SplashScreenActivity");
startActivity(intent);

Solution 10 - Android

// This works on Android Lollipop 5.0.2

public static boolean launchApp(Context context, String packageName) {

    final PackageManager manager = context.getPackageManager();
    final Intent appLauncherIntent = new Intent(Intent.ACTION_MAIN);
    appLauncherIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    List<ResolveInfo> resolveInfos = manager.queryIntentActivities(appLauncherIntent, 0);
    if ((null != resolveInfos) && (!resolveInfos.isEmpty())) {
        for (ResolveInfo rInfo : resolveInfos) {
            String className = rInfo.activityInfo.name.trim();
            String targetPackageName = rInfo.activityInfo.packageName.trim();
            Log.d("AppsLauncher", "Class Name = " + className + " Target Package Name = " + targetPackageName + " Package Name = " + packageName);
            if (packageName.trim().equals(targetPackageName)) {
                Intent intent = new Intent();
                intent.setClassName(targetPackageName, className);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                Log.d("AppsLauncher", "Launching Package '" + packageName + "' with Activity '" + className + "'");
                return true;
            }
        }
    }
    return false;
}

Solution 11 - Android

If you want to open another application and it is not installed you can send it to the Google App Store to download

  1. First create the openOtherApp method for example

     public static boolean openOtherApp(Context context, String packageName) {
         PackageManager manager = context.getPackageManager();
          try {
             Intent intent = manager.getLaunchIntentForPackage(packageName);
             if (intent == null) {
                 //the app is not installed
                 try {
                     intent = new Intent(Intent.ACTION_VIEW);
                     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                     intent.setData(Uri.parse("market://details?id=" + packageName));
                     context.startActivity(intent);
                 } catch (ActivityNotFoundException e) {
                     //throw new ActivityNotFoundException();
                     return false;
                 }
    
              }
              intent.addCategory(Intent.CATEGORY_LAUNCHER);
              context.startActivity(intent);
              return true;
         } catch (ActivityNotFoundException e) {
             return false;
         }
    
     }
    

2.- Usage

openOtherApp(getApplicationContext(), "com.packageappname");

Solution 12 - Android

Since applications aren't allowed to change many of the phone settings, you can open a settings activity just like another application.

Look at you LogCat output after you actually modified the setting manually:

INFO/ActivityManager(1306): Starting activity: Intent { act=android.intent.action.MAIN cmp=com.android.settings/.DevelopmentSettings } from pid 1924

Then use this to display the settings page from your app:

String SettingsPage = "com.android.settings/.DevelopmentSettings";

try
{
Intent intent = new Intent(Intent.ACTION_MAIN);             
intent.setComponent(ComponentName.unflattenFromString(SettingsPage));             
intent.addCategory(Intent.CATEGORY_LAUNCHER );             
startActivity(intent); 
}
catch (ActivityNotFoundException e)
{
 log it
}

Solution 13 - Android

For API level 3+, nothing more then one line of code:

Intent intent = context.getPackageManager().getLaunchIntentForPackage("name.of.package");

Return a CATEGORY_INFO launch Intent (apps with no launcher activity, wallpapers for example, often use this to provide some information about app) and, if no find it, returns the CATEGORY_LAUNCH of package, if exists.

Solution 14 - Android

If you're attempting to start a SERVICE rather than activity, this worked for me:

Intent intent = new Intent();
intent.setClassName("com.example.otherapplication", "com.example.otherapplication.ServiceName");
context.startService(intent);

If you use the intent.setComponent(...) method as mentioned in other answers, you may get an "Implicit intents with startService are not safe" warning.

Solution 15 - Android

Alternatively you can also open the intent from your app in the other app with:

Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

where uri is the deeplink to the other app

Solution 16 - Android

Use following:

String packagename = "com.example.app";
startActivity(getPackageManager().getLaunchIntentForPackage(packagename));

Solution 17 - Android

Launch an application from another application on Android

  Intent launchIntent = getActivity.getPackageManager().getLaunchIntentForPackage("com.ionicframework.uengage");
        startActivity(launchIntent);

Solution 18 - Android

Try this code, Hope this will help you. If this package is available then this will open the app or else open the play store for downloads

    String  packageN = "aman4india.com.pincodedirectory";

            Intent i = getPackageManager().getLaunchIntentForPackage(packageN);
            if (i != null) {
                i.addCategory(Intent.CATEGORY_LAUNCHER);
                startActivity(i);
            } else {
                try {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageN)));
                }
                catch (android.content.ActivityNotFoundException anfe) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + packageN)));
                }
            }

Solution 19 - Android

You can use this command to find the package names installed on a device:

adb shell pm list packages -3 -f

Reference: http://www.aftvnews.com/how-to-determine-the-package-name-of-an-android-app/

Solution 20 - Android

Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName("package_name","package_name.class_name"));
        intent.putExtra("grace", "Hi");
        startActivity(intent);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndersWidView Question on Stackoverflow
Solution 1 - AndroidChristopherView Answer on Stackoverflow
Solution 2 - AndroidgvaishView Answer on Stackoverflow
Solution 3 - AndroidzawhtutView Answer on Stackoverflow
Solution 4 - AndroidMuzikantView Answer on Stackoverflow
Solution 5 - AndroidinversusView Answer on Stackoverflow
Solution 6 - Androidpeter.bartosView Answer on Stackoverflow
Solution 7 - AndroidSwethaView Answer on Stackoverflow
Solution 8 - AndroidOleksandr BView Answer on Stackoverflow
Solution 9 - AndroidKCNView Answer on Stackoverflow
Solution 10 - AndroidAnil KongoviView Answer on Stackoverflow
Solution 11 - AndroidVladimir SalgueroView Answer on Stackoverflow
Solution 12 - AndroidTaryView Answer on Stackoverflow
Solution 13 - AndroidRenascienzaView Answer on Stackoverflow
Solution 14 - AndroidDuncView Answer on Stackoverflow
Solution 15 - AndroidelectrobabeView Answer on Stackoverflow
Solution 16 - AndroidlarsaarsView Answer on Stackoverflow
Solution 17 - AndroidAshutosh SrivastavaView Answer on Stackoverflow
Solution 18 - AndroidAMAN SINGHView Answer on Stackoverflow
Solution 19 - AndroidJPritchard9518View Answer on Stackoverflow
Solution 20 - AndroidManzerView Answer on Stackoverflow