一、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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# ipc
# bundle
# android
# binder
# Bundle进行IPC
# 详解Android跨进程IPC通信AIDL机制原理
# Android IPC机制Messenger实例详解
# Android系统进程间通信(IPC)机制Binder中的Server和Client获得Servic
# 浅谈Service Manager成为Android进程间通信(IPC)机制Binder守护进程之路
# Android clipChildren属性实例详解
# android IPC之binder通信机制
# Android IPC机制ACtivity绑定Service通信代码实例
# 实现了
# 都是
# 下载地址
# 三大
# 它可以
# 不支持
# 它在
# 具体内容
# 大家多多
# 就可以
# 序列化
# version
# activity_main
# xmlns
# http
# LinearLayout
# encoding
# utf
# lt
# activity
相关文章:
网站制作多少钱一个,建一个论坛网站大约需要多少钱?
网站好制作吗知乎,网站开发好学吗?有什么技巧?
无锡营销型网站制作公司,无锡网选车牌流程?
如何用VPS主机快速搭建个人网站?
视频网站制作教程,怎么样制作优酷网的小视频?
建站三合一如何选?哪家性价比更高?
巅云智能建站系统:可视化拖拽+多端适配+免费模板一键生成
C#怎么使用委托和事件 C# delegate与event编程方法
如何通过山东自助建站平台快速注册域名?
微网站制作教程,我微信里的网站怎么才能复制到浏览器里?
建站之星代理费用多少?最新价格详情介绍
如何选择最佳自助建站系统?快速指南解析优劣
威客平台建站流程解析:高效搭建教程与设计优化方案
建站之星安装路径如何正确选择及配置?
建站DNS解析失败?如何正确配置域名服务器?
公司门户网站制作流程,华为官网怎么做?
大学网站设计制作软件有哪些,如何将网站制作成自己app?
C++ static_cast和dynamic_cast区别_C++静态转换与动态类型安全转换
如何在局域网内绑定自建网站域名?
,交易猫的商品怎么发布到网站上去?
如何快速搭建高效WAP手机网站吸引移动用户?
如何通过西部数码建站助手快速创建专业网站?
建站主机与虚拟主机有何区别?如何选择最优方案?
番禺网站制作公司哪家值得合作,番禺图书馆新馆开放了吗?
logo在线制作免费网站在线制作好吗,DW网页制作时,如何在网页标题前加上logo?
如何处理“XML格式不正确”错误 常见XML well-formed问题解决方法
网站制作公司排行榜,四大门户网站排名?
制作企业网站建设方案,怎样建设一个公司网站?
建站之星如何优化SEO以实现高效排名?
SQL查询语句优化的实用方法总结
如何在宝塔面板中创建新站点?
北京营销型网站制作公司,可以用python做一个营销推广网站吗?
宝塔面板如何快速创建新站点?
高端云建站费用究竟需要多少预算?
湖州网站制作公司有哪些,浙江中蓝新能源公司官网?
建站之星多图banner生成与模板自定义指南
如何彻底删除建站之星生成的Banner?
网站制作培训多少钱一个月,网站优化seo培训课程有哪些?
如何有效防御Web建站篡改攻击?
购物网站制作公司有哪些,哪个购物网站比较好?
c# 服务器GC和工作站GC的区别和设置
小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?
如何在IIS7上新建站点并设置安全权限?
如何在云虚拟主机上快速搭建个人网站?
广东专业制作网站有哪些,广东省能源集团有限公司官网?
高端建站三要素:定制模板、企业官网与响应式设计优化
h5网站制作工具有哪些,h5页面制作工具有哪些?
杭州银行网站设计制作流程,杭州银行怎么开通认证方式?
已有域名和空间如何搭建网站?
如何获取上海专业网站定制建站电话?
*请认真填写需求信息,我们会在24小时内与您取得联系。