Created
November 28, 2013 16:16
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
That would be handled in the target app, a timer countdown that triggers an activity finish after 10 seconds
Code is working...but its not working on Above android 10 version..is there any solution
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);
}
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
hello, thanks for code! but i have a question! i run a com.example.myapp, but how to finish or kill it after 10 sec?