用于显式启动外部应用的代码

CL2*_*L22 22 android

从我的一个应用程序,我正在尝试推出另一个.我想使用明确的意图.

ComponentName cn = new ComponentName("com.myOtherApp", "OtherAppActivity");
Intent intent = new Intent();
intent.setComponent(cn);
context.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

但是,当我运行该代码时,它会询问我是否在清单中声明了该活动.但是,当我将以下内容放入清单时,我得到了同样的错误:

<activity android:name="com.myOtherApp.OtherAppActivity">
</activity>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

谢谢

Squ*_*onk 25

尝试这样的事......

在'myOtherApp'的清单中,使用具有公司特定意图的'OtherAppActivity'的意图过滤器,例如......

<activity
    android:name=".OtherAppActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="com.mycompany.DO_SOMETHING" />
    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

然后,在"通话"应用中,使用...

Intent intent = new Intent();
intent.setAction("com.mycompany.DO_SOMETHING");
context.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

  • 请注意,intent过滤器具有include <category android:name ="android.intent.category.DEFAULT"/> (4认同)

Jon*_*nce 19

我遇到了这个问题并搜索了几个小时寻找解决方案.终于找到了它:http://www.krvarma.com/2010/08/launching-external-applications-in-android.该链接显示了如何使用包管理器启动您只有包名称的任何应用程序:

PackageManager pm = this.getPackageManager();

try
{
  Intent it = pm.getLaunchIntentForPackage(sName);

  if (null != it)
    this.startActivity(it);
}

catch (ActivityNotFoundException e)
{
}
Run Code Online (Sandbox Code Playgroud)


Sog*_*ger 17

您需要在新ComponentName的第二个参数中指定完全限定的类名,如下所示:

ComponentName cn = new ComponentName("com.myOtherApp", "com.myOtherApp.OtherAppActivity");
Run Code Online (Sandbox Code Playgroud)

我认为这是因为清单中的包名称和活动名称不一定必须具有相同的包路径,因此新的ComponentName调用不会推断类名称第二个参数以包名称第一个参数为前缀.