全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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中开启多进程

<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 


相关文章: 如何基于云服务器快速搭建网站及云盘系统?  如何选择网络建站服务器?高效建站必看指南  表情包在线制作网站免费,表情包怎么弄?  如何通过山东自助建站平台快速注册域名?  如何在Windows虚拟主机上快速搭建网站?  焦点电影公司作品,电影焦点结局是什么?  ,有什么在线背英语单词效率比较高的网站?  如何通过网站建站时间优化SEO与用户体验?  长沙企业网站制作哪家好,长沙水业集团官方网站?  专业的网站制作设计是什么,如何制作一个企业网站,建设网站的基本步骤有哪些?  企业宣传片制作网站有哪些,传媒公司怎么找企业宣传片项目?  湖州网站制作公司有哪些,浙江中蓝新能源公司官网?  测试制作网站有哪些,测试性取向的权威测试或者网站?  昆明高端网站制作公司,昆明公租房申请网上登录入口?  如何选择长沙网站建站模板?H5响应式与品牌定制哪个更优?  网站制作公司,橙子建站是合法的吗?  建站上传速度慢?如何优化加速网站加载效率?  哈尔滨网站建设策划,哈尔滨电工证查询网站?  兔展官网 在线制作,怎样制作微信请帖?  电商网站制作价格怎么算,网上拍卖流程以及规则?  正规网站制作公司有哪些,目前国内哪家网页网站制作设计公司比较专业靠谱?口碑好?  ,怎么在广州志愿者网站注册?  单页制作网站有哪些,朋友给我发了一个单页网站,我应该怎么修改才能把他变成自己的呢,请求高手指点迷津?  建站与域名管理如何高效结合?  电商平台网站制作流程,电商网站如何制作?  可靠的网站设计制作软件,做网站设计需要什么样的电脑配置?  网站企业制作流程,用什么语言做企业网站比较好?  制作网站的软件下载免费,今日头条开宝箱老是需要下载怎么回事?  如何选择高性价比服务器搭建个人网站?  高防服务器租用首荐平台,企业级优惠套餐快速部署  临沂网站制作公司有哪些,临沂第四中学官网?  阿里云网站制作公司,阿里云快速搭建网站好用吗?  Swift中循环语句中的转移语句 break 和 continue  视频网站制作教程,怎么样制作优酷网的小视频?  广州网站制作公司哪家好一点,广州欧莱雅百库网络科技有限公司官网?  建站之星各版本价格是多少?  深圳企业网站制作设计,在深圳如何网上全流程注册公司?  东莞市网站制作公司有哪些,东莞找工作用什么网站好?  建站之星安全性能如何?防护体系能否抵御黑客入侵?  建站之星代理平台如何选择最佳方案?  建站主机选择指南:服务器配置与SEO优化实战技巧  宠物网站制作html代码,有没有专门介绍宠物如何养的网站啊?  建站主机是什么?如何选择适合的建站主机?  如何通过西部数码建站助手快速创建专业网站?  头像制作网站在线观看,除了站酷,还有哪些比较好的设计网站?  定制建站流程解析:需求评估与SEO优化功能开发指南  建站之星云端配置指南:模板选择与SEO优化一键生成  如何快速上传建站程序避免常见错误?  网站建设制作需要多少钱费用,自己做一个网站要多少钱,模板一般多少钱?  如何安全更换建站之星模板并保留数据? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。