How to leave the current app and launch an activity of another app?

泄露秘密 提交于 2021-01-29 09:03:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!