您的位置:首页 > 其它

外部程序调用应用Activity方法简介

2014-09-15 16:32 453 查看
如果想要在一个程序中,打开另外一个应用的Activity(例如在音乐播放页面打开音效Eq控制页面),就需要调用到此方法。

1、使用自定义的Action

A程序中的代码为:

Intent intent = new Intent();
intent.setAction("com.test.action.PLAYER");
startActivity(intent);


B程序中的AndroidManifest.xml中启动Activity的intent-filter:

<intent-filter>
<span style="white-space:pre">	</span><action android:name="android.intent.action.MAIN" />
<span style="white-space:pre">	</span><action android:name="com.test.action.PLAYER" />
<span style="white-space:pre">	</span><category android:name="android.intent.category.DEFAULT" /><!--必须,否则无效-->
<span style="white-space:pre">	</span><category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
2、使用包类名

A程序中调用的代码为:

Intent intent = new Intent();
intent.setClassName("com.test", "com.test.Player");
startActivity(intent);
intent.setClassName(arg1,arg2)中的arg1是被调用程序B的包名,arg2是B程序中目的activity的完整类名。

或者代码也可以这样写:

Intent intent = new Intent();
ComponentName comp = new ComponentName("com.test", "com.test.Player" );
intent.setComponent(comp);
startActivity(intent);
B程序(被调用)中的AndroidManifest.xml中启动Activity的intent-filter不需要特别加入其它信息,如下即可:

<intent-filter>
<span style="white-space:pre">	</span><action android:name="android.intent.action.MAIN" />
<span style="white-space:pre">	</span><category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: