简体   繁体   中英

Launch third party app from Service

I need to launch an activity from a service. I tried using:

public void openApplication(String packageName){
      Intent intent = new Intent(packageName);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      this.startActivity(intent);
    }

But i get this error:

E/AndroidRuntime(8563):android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.encrypted flg=0x10000000 }
04-30 11:55:07.507: E/AndroidRuntime(8563):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1765)
04-30 11:55:07.507: E/AndroidRuntime(8563):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)

Any idea?

Try this code instead. You need to get a launch Intent for the package:

public void openApplication(String packageName) {
    PackageManager pm = getPackageManager();
    Intent intent = pm.getLaunchIntentForPackage(packageName);
    startActivity(intent);
}

You've created an Intent with a certain 'action'. Find the correct action String that's handled by the desired application's IntentFilter. The packageName is not the same as the action.

If multiple applications should have the same action defined in an IntentFilter, the system will popup an IntentChooser where all these applications are shown in.

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