Code to launch external app explicitly

后端 未结 6 866
闹比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:34

    Try something like this...

    In the manifest for 'myOtherApp' use an intent filter for 'OtherAppActivity' with a company specific intent, example...

    <activity
        android:name=".OtherAppActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.mycompany.DO_SOMETHING" />
        </intent-filter>
    </activity>
    

    Then, in the 'calling' app, use...

    Intent intent = new Intent();
    intent.setAction("com.mycompany.DO_SOMETHING");
    context.startActivity(intent);
    
    0 讨论(0)
  • 2020-12-01 14:40

    I had this problem and searched for hours looking for a solution. Finally found it: http://www.krvarma.com/2010/08/launching-external-applications-in-android. That link shows how to use the package manager to launch any application for which you have simply the package name:

    PackageManager pm = this.getPackageManager();
    
    try
    {
      Intent it = pm.getLaunchIntentForPackage(sName);
    
      if (null != it)
        this.startActivity(it);
    }
    
    catch (ActivityNotFoundException e)
    {
    }
    
    0 讨论(0)
  • 2020-12-01 14:42

    In addition to @Sogger answer thing to remember is if you receiver class is com.myOtherApp.receiver.OtherAppActivity and package mentioned in AndroidManifest is com.myOtherApp your code will be

    ComponentName cn = new ComponentName("com.myOtherApp", "com.myOtherApp.receiver.OtherAppActivity");
    
    0 讨论(0)
  • 2020-12-01 14:46

    You need to specify the fully qualified class name in the second parameter of new ComponentName like this:

    ComponentName cn = new ComponentName("com.myOtherApp", "com.myOtherApp.OtherAppActivity");
    

    I think this is because the package name in the manifest and the activity name don't necessarily have to have the same package path, so the new ComponentName call doesn't infer the class name second parameter is prefixed by the package name first parameter.

    0 讨论(0)
  • 2020-12-01 14:54

    Create the intent as action.Main and add the launcher category to it:

    Intent intent = new Intent("android.intent.action.MAIN");
    intent.addCategory("android.intent.category.LAUNCHER");
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题