Android跨进程通信的四种方式

来源:互联网 发布:复杂网络理论及其应用 编辑:程序博客网 时间:2024/06/11 17:58

Android系统中应用程序之间不能共享内存,在android SDK中提供了4种用于跨进程通讯的方式。这4种方式正好对应于android系统中4种应用程序组件:Activity、Content Provider、Broadcast和Service。

1、其中Activity可以跨进程调用其他应用程序的Activity
2、Content Provider可以跨进程访问其他应用程序中的数据(以Cursor对象形式返回)
3、Broadcast可以向android系统中所有应用程序发送广播,而需要跨进程通讯的应用程序可以监听这些广播。
4、Service和Content Provider类似,也可以访问其他应用程序中的数据,Content Provider返回的是Cursor对象,而Service返回的是Java对象,这种可以跨进程通讯的服务叫AIDL服务。

Activity 通信

同一个进程访问,需要指定Context对象和Activity的Class对象,代码如下:

Intent intent = new Intent(this, SecMainActivity.class);startActivity(intent);

跨进程访问,跨进程访问并不需要指定Context对象和Activity的 Class对象,而需要指定的是要访问的Activity所对应的Action(一个字符串)。有些Activity还需要指定一个Uri(通过 Intent构造方法的第2个参数指定), 启动拨打电话的Activity, 代码如下:

Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:13716309123"));startActivity(callIntent);

在调用拨号程序的代码中使用了一个Intent.ACTION_CALL常量,该常量的定义如下:

/** * Activity Action: Perform a call to someone specified by the data. * <p>Input: If nothing, an empty dialer is started; else {@link #getData} * is URI of a phone number to be dialed or a tel: URI of an explicit phone * number. * <p>Output: nothing. * * <p>Note: there will be restrictions on which applications can initiate a * call; most applications should use the {@link #ACTION_DIAL}. * <p>Note: this Intent <strong>cannot</strong> be used to call emergency * numbers.  Applications can <strong>dial</strong> emergency numbers using * {@link #ACTION_DIAL}, however. * * <p>Note: if you app targets {@link android.os.Build.VERSION_CODES#M M} * and above and declares as using the {@link android.Manifest.permission#CALL_PHONE} * permission which is not granted, then attempting to use this action will * result in a {@link java.lang.SecurityException}. */@SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)public static final String ACTION_CALL = "android.intent.action.CALL";

如果在应用程序中要共享某个Activity,需要为这个 Activity指定一个字符串ID,也就是Action。也可以将这个Action看做这个Activity的key。在其他的应用程序中只要通过这个 Action就可以找到与Action对应的Activity,并通过startActivity方法来启动这个Activity。
如何将应用程序的Activity共享出来
1、 在AndroidManifest.xml文件中指定Action。使用标签在android:name属性中指定Action
2、在AndroidManifest.xml文件中指定访问协议。在指定Uri(Intent类的第2个参数)时需要访问协议。访问协议需要使 用标签的android:scheme属性来指定。如果该属性的值是“tel”,那么Uri就应该是“tel://Uri的主体 部分”,访问协议是Uri的开头部分。
3、通过getIntent().getData().getHost()方法获得协议后的Uri的主体部分。这个Host只是个称谓,并不一定是主机名。
4、从Bundle对象中获得其他应用程序传递过来的数据。
5、获取数据后做相应的处理。
打开AndroidManifest.xml文件,添加一个标签,并重新定义了 Main的相应属性。AndroidManifest.xml文件的内容如下:

<activity android:name=".SecMainActivity">    <intent-filter>        <action android:name="com.owen.SecMainActivity" />        <data android:scheme="owen" />    </intent-filter></activity>

在配置AndroidManifest.xml时要注意,不能在同一个中配置多个动作,否则会覆盖MAIN动作以使该程序无法正常启动(虽然其他应用程序调用Main是正常的)。
一般标签的android:name属性值可以设成android.intent.category.DEFAULT。

如何获取其他应用传递过来的数据

if (getIntent().getData() != null) {    String host = getIntent().getData().getHost();    Bundle bundle = getIntent().getExtras();    String value = bundle.getString("value");}如何启动ActivityIntent intent = new Intent("com.owen.SecMainActivity", Uri.parse("owen://start Activity"));intent.putExtra("value", "启动成功");

Content Provider

Android 创建及调用自己的 ContentProvider

广播(Broadcast)

Android 中的消息传递-广播机制

AIDL服务

Android AIDL 使用详解

原创粉丝点击