Skip to content

Instantly share code, notes, and snippets.

@201949
Last active January 21, 2024 13:34
Show Gist options
  • Save 201949/295f6402537d6da28f8176eb32d43aed to your computer and use it in GitHub Desktop.
Save 201949/295f6402537d6da28f8176eb32d43aed to your computer and use it in GitHub Desktop.
Launch another Android app using an intent with a package name, with a fix for Android 13 and later

This Java method allow to launch another Android app using an intent with a package name, with a fix for Android 13 and later. Add section to your manifest or permission QUERY_ALL_PACKAGES. Please note the Google Play Developer Policy regarding restrictions on the use of this permission.

//Launch another Android app using an intent with a package name, with a fix for Android 13 and later. Java code.
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, 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);
}
<?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>
<!-- OR -->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
...
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
...
</manifest>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment