Android之使用Bundle进行IPC详解
程序员文章站
2024-02-24 18:50:40
一、bundle进行ipc介绍
四大组件中的三大组件(activity、service、receiver)都是支持在intent中传递bundle数据的,由于bund...
一、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中开启多进程
<activity ... android:process=":remote" />
三、小案例
1.修改activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?> <linearlayout 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" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="bundler"> </textview> <button android:id="@+id/bundler_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="send message"> </button> </linearlayout>
2.添加activity_third.xml文件
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="at activity third" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="activity third" /> </linearlayout>
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文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zhangmiao.ipcdemo"> <uses-permission android:name="android.permission.write_external_storage" /> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity" android:label="@string/app_name" android:launchmode="standard" android:theme="@style/apptheme.noactionbar"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".thirdactivity" android:configchanges="screenlayout" android:label="@string/app_name" android:process=":remote" /> </application> </manifest>
完整代码下载地址:demo
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。