Android中app之间的交互(二,使用ComponentName)

本篇笔记中我在简单介绍一下使用ComponentName来与当前应用之外的应用进行交互。

1,ComponentName介绍

在介绍Component之前,我们首先来了解ComponentName这个类;ComponentName与Intent同位于android.content包下,我们从Android官方文档中可以看到,这个类主要用来定义可见一个应用程序组件,例如:Activity,Service,BroadcastReceiver或者ContentProvider。

那么,如何用ComponentName来定义一个组件呢。

这是ComponentName的构造函数:ComponentName(String pkg,String cls)

        我们知道在Android应用程序中如果要详细描述一个组件我们需要知道该组件所在的应用包名,也就是在AndroidManifest.xml文件中manifest根结点下的package=“XXX.XXXXX.XXXXX",还有组件在应用程序中的完整路径名,拿Activity来说,也就是activity节点中name属性的值。因此到这里我们也就明白了可以使用ComponentName来封装一个组件的应用包名和组件的名字。

        我们已经知道,在Android中组件之间的交流往往使用意图(Intent)来完成的,那么在Intent中有一个方法可以封装一个ComponentName,最后我们在使用意图去完成我们需要实现的功能。

2,举例:启动另一个APK

        首先我们要新建两个Android应用程序,appsend和appreceiver。

        appreceiver的AndroidMainfest.xml

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.    <span style="color:#cc0000;"> <strong>package="com.example.appreceiver"</strong></span>  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="11"  
  9.         android:targetSdkVersion="18" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.            <strong><span style="color:#ff0000;"> android:name="com.example.appreceiver.MainActivity"</span></strong>  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.   
  27. </manifest>  

appsend中的启动Activity的片段:


public void button(View view) {  
	ComponentName cn=new ComponentName("com.example.appreceiver", "com.example.appreceiver.MainActivity");
	Intent intent = new Intent();  
	intent.setComponent(cn);
	startActivityForResult(intent, 2);  
}  

3,举例:启动另一个APK

要被启动的APK的 AndroidMainfest.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.autochips.avin" >
    
    <application
        android:icon="@drawable/mm_icon"
        android:label="@string/app_name" 
        android:name="com.autochips.avin.AVINApp">  
                 
        <activity
            android:screenOrientation="landscape"
            android:process="android.process.avin"
            android:taskAffinity="android.task.avin"
            android:configChanges="keyboardHidden|orientation"
            android:label="@string/app_name"
            android:name="com.autochips.avin.AVInActivity"
            android:theme="@android:style/Theme.NoTitleBar"
            android:background="@color/background">
            
            <intent-filter>
                <!-- 当前界面显示 -->
                <action android:name="android.intent.action.MAIN" />  
				<!-- 在桌面上创建图标 -->
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>         
        </activity>
        
        <activity
            android:screenOrientation="landscape"
            android:label="@string/app_name"
            android:name="com.autochips.avin.AVInSelectActivity" 
            android:theme="@android:style/Theme.NoTitleBar">        
        </activity>
        
    </application>

    <uses-permission android:name="android.permission.RESTART_PACKAGES" />
    <uses-permission android:name="android.permission.FORCE_STOP_PACKAGES" />
    <uses-permission android:name="android.permission.INTERNAL_SYSTEM_WINDOW"/>

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true" >
    </supports-screens>

</manifest>

当前APK中需要启动另一个APK的代码如下:

<pre name="code" class="java">
Intent intent = new Intent();
ComponentName name = new ComponentName("com.autochips.avin", "com.autochips.avin.AVInActivity");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
	mContext.startActivity(intent);
} catch (Exception e) {
	e.printStackTrace();
}


 

 
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值