问题
I have a simple Android app that needs to launch another app under certain condition, and I will need to check the condition upon the app launch. That is, either continue to launch my app or just launch another app:
if (A == true) {
launch another activity of another app
leave the current app without creating the main activity
} else {
launch the main activity of the current app
}
Could anyone please let me know how to deal with A == true case? I am able to launch another app's activity but I have trouble leaving the current app without even opening the main activity.
Any help will be greatly appreciated!
回答1:
You can launch other application using following intent
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.abc");//pass the packagename of app you want to open
startActivity( LaunchIntent );
If you don't know the package name of application that you wanted to launch then try your hand on
PackageManager pm;
pm = getPackageManager();
// get a list of installed apps.
packages = pm.getInstalledApplications(0);
Pass the packagename of app you want to open You can use this if A == true
else You can launch the MainActivity as
startActivity(new Intent(CurrentActivity.this,MainActivity.class));
回答2:
If you want to start another activity of another app without the normal IntentFilter then the easiest solutions is:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.anotherapp.package","com.anotherapp.package.MainActivity"));
startActivity(intent);
Of course, you would need to know the package name
and the Activity
name you want to start
As for finishing your application call
finishAndRemoveTask();
来源:https://stackoverflow.com/questions/50017762/how-to-leave-the-current-app-and-launch-an-activity-of-another-app