设为首页 加入收藏

TOP

Android之使用Bundle进行IPC(一)
2017-10-13 09:42:19 】 浏览:2312
Tags: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)
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android中开发工具Android Studio.. 下一篇Android 几种消息推送方案总结

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目