简体   繁体   中英

Use intent to launch app

I am implementing the intent to launch an application inside service. It's working fine when I pass other applications package names, but it's showing exception when I call the same package name where I have implemented the intent. It's showing like " have you declared it manifest or not ?". It's already declared in manifest.

Intent nextIntent = new Intent(Intent.ACTION_MAIN);
nextIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
nextIntent.setComponent(new ComponentName("com.example.app1","com.example.app1.MainActivity"));
startActivity(nextIntent);

If I replace the package name , it's working fine. How to do it?

start the activity in the standard way:

Intent nextIntent = new Intent(context_reference, MainActivity.class);
nextIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
nextIntent.setAction(Intent.ACTION_MAIN);
startActivity(nextIntent);

If you pass strings, you're screwed in case of a refactoring - if you forget to modify the string as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM