Code to launch external app explicitly

后端 未结 6 896
闹比i
闹比i 2020-12-01 14:21

From one of my apps, I\'m trying to launch another. I want to use an explicit intent.

ComponentName cn = new ComponentName(\"com.myOtherApp\", \"OtherAppActi         


        
6条回答
  •  悲&欢浪女
    2020-12-01 14:58

    As of API23, you could use the method ComponentName.createRelative(String pkg, String cls) and do:

    ComponentName cn = new ComponentName(ComponentName.createRelative("com.myOtherApp", ".OtherAppActivity"));
    Intent intent = new Intent();
    intent.setComponent(cn);
    context.startActivity(intent);
    

    This way you can create a ComponentName object using a relative class path. Mind the dot in the start of the class path. It is necessary to indicate that the method should treat the second argument as a relative path. Just as @Sogger mentioned, the ComponentName constructor constraints the class parameter to be an absolute path.

    Note also that by this manner, you are using explicit intents and you don't have to insert any additional intent filters to the destination activity.

提交回复
热议问题