android bundle机制6,Android之使用Bundle进行IPC详解

一、Bundle进行IPC介绍

四大组件中的三大组件(Activity、Service、Receiver)都是支持在Intent中传递Bundle数据的,由于Bundle实现了Parcelable接口,所以它可以方便地在不同的进程之间传输。当然,传输的数据必须能够被序列化,比如基本类型、实现了Parcelable接口的对象、实现了Serializable接口的对象以及一些Android支持的特殊对象,具体内容可以看Bundle这个类,就可以看到所有它支持的类型。Bundle不支持的类型无法通过它在进程间传递数据。

二、使用方法

1.打包数据发送

Intent intent1 = new Intent(MainActivity.this, ThirdActivity.class);

Bundle bundle = new Bundle();

bundle.putCharSequence("name", "zhangmiao");

bundle.putInt("age", 20);

intent1.putExtras(bundle);

startActivity(intent1);

2.接受数据

Intent intent = getIntent();

Bundle bundle = intent.getExtras();

String name = bundle.getString("name");

int age = bundle.getInt("age");

3.在AndroidManifest.xml中开启多进程

...

android:process=":remote" />

三、小案例

1.修改activity_main.xml文件

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true"

tools:context="com.zhangmiao.ipcdemo.MainActivity"

android:orientation="vertical"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:text="Bundler">

android:id="@+id/bundler_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="send message">

2.添加activity_third.xml文件

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:text="at activity Third" />

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Activity Third" />

3.添加ThirdActivity类

package com.zhangmiao.ipcdemo;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.widget.TextView;

/**

* Created by zhangmiao on 2016/12/27.

*/

public class ThirdActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_third);

Intent intent = getIntent();

Bundle bundle = intent.getExtras();

String name = bundle.getString("name");

int age = bundle.getInt("age");

TextView textView = (TextView) findViewById(R.id.textView1);

textView.setText("name:" + name + ",age:" + age);

}

}

4.修改MainActivity类

package com.zhangmiao.ipcdemo;

import android.content.ComponentName;

import android.content.Context;

import android.content.Intent;

import android.content.ServiceConnection;

import android.os.Bundle;

import android.os.Handler;

import android.os.IBinder;

import android.os.Message;

import android.os.Messenger;

import android.os.RemoteException;

import android.support.v7.app.AppCompatActivity;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.io.Serializable;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.bundler_button);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent1 = new Intent(MainActivity.this, ThirdActivity.class);

Bundle bundle = new Bundle();

bundle.putCharSequence("name", "zhangmiao");

bundle.putInt("age", 20);

intent1.putExtras(bundle);

startActivity(intent1);

}

});

}

}

5.修改AndroidManifest.xml文件

package="com.zhangmiao.ipcdemo">

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:supportsRtl="true"

android:theme="@style/AppTheme">

android:name=".MainActivity"

android:label="@string/app_name"

android:launchMode="standard"

android:theme="@style/AppTheme.NoActionBar">

android:name=".ThirdActivity"

android:configChanges="screenLayout"

android:label="@string/app_name"

android:process=":remote" />

完整代码下载地址:demo

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值