Skip to content

Instantly share code, notes, and snippets.

@dominicthomas
Created November 28, 2013 16:16
Show Gist options
  • Select an option

  • Save dominicthomas/7694379 to your computer and use it in GitHub Desktop.

Select an option

Save dominicthomas/7694379 to your computer and use it in GitHub Desktop.
Launch another app using an intent with package name if app has a launcher activity or using package name and class name of main activity.
// only works if app has a launcher activity
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.yourapp");
startActivity(launchIntent);
// works if we know the name of the main activity, even if not a launcher
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.example.yourapp", "com.example.yourapp.MainActivity");
startActivity(intent);
@dominicthomas

Copy link
Copy Markdown
Author

That would be handled in the target app, a timer countdown that triggers an activity finish after 10 seconds

@faizifayiz

Copy link
Copy Markdown

Code is working...but its not working on Above android 10 version..is there any solution

@201949

201949 commented Jan 21, 2024

Copy link
Copy Markdown

Code is working...but its not working on Above android 10 version..is there any solution

Hello!
My working solution for Android 13 is:

add this section in AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

  ...
  <queries> <!-- name the required packages -->
    <package android:name="com.your.company.your.app"/>
    <package android:name="com.other.company.other.app"/>
  </queries>
  ...

</manifest>

and my method in Java is:

public void startNewActivity(Context context, String packageName) {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        //Fix for Android 13
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
        }

        if (intent == null) {
            // Bring user to the market or let them choose an app, as the package is not installed
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("market://details?id=" + packageName));
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

@steadfasterX

Copy link
Copy Markdown

you made my day @dominicthomas !! thx for sharing this..

only change needed for me was using context.startActivity(intent); instead of startActivity(intent); (Android14)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment