ComponentName,顾名思义,就是组件名称,通过调用Intent中的setComponent方法,我们可以打开另外一个应用中的Activity或者服务。
实例化一个ComponentName需要两个参数,第一个参数是要启动应用的包名称,这个包名称是指清单文件中列出的应用的包名称:
第二个参数是你要启动的Activity或者Service的全称(包名+类名),代码如下:
启动一个Activity:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.otherapp",
"com.example.otherapp.MainActivity2"));
startActivity(intent);启动一个Service:
Intent it = new Intent();
it.setComponent(new ComponentName("com.example.otherapp",
"com.example.otherapp.MyService"));
startService(it);注意
如果你要的启动的其他应用的Activity不是该应用的入口Activity,那么在清单文件中,该Activity节点一定要加上android:exported="true",表示允许其他应用打开,对于所有的Service,如果想从其他应用打开,也都要加上这个属性:
<service
android:name="com.example.otherapp.MyService"
android:exported="true" >
</service>
<activity
android:name="com.example.otherapp.MainActivity2"
android:exported="true" >
</activity>那么为什么入口Activity不用添加这个属性就可以被其他应用启动呢?我们来看一段入口Activity的注册代码:
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>明白否?
有问题欢迎交流...
版权声明:本文为博主原创文章,未经博主允许不得转载。若有错误地方,还望批评指正,不胜感激。
原文:http://blog.csdn.net/u012702547/article/details/49557905