Android之使用Bundle进行IPC详解

 更新时间:2017年01月01日 12:40:21   作者:zhangmiao14  
这篇文章主要介绍了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

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

相关文章

  • Android拍照和获取相册图片

    Android拍照和获取相册图片

    这篇文章主要为大家详细介绍了Android拍照和获取相册图片的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Android下的EXIF是什么

    Android下的EXIF是什么

    这篇文章主要为大家详细介绍了Android下的EXIF是什么,Exif(Exchangeable Image File 可交换图像文件)是一种图象文件格式,它的数据存储与JPEG格式是完全相同的,想要深入了解的朋友可以参考一下
    2016-04-04
  • 实例详解用户输入 i. 检测常用手势

    实例详解用户输入 i. 检测常用手势

    通过本段代码给大家介绍当用户输入i检测常用手势的相关内容,代码简单易懂,感兴趣的朋友一起学习吧
    2016-01-01
  • Android 实现ViewPager边界回弹效果实例代码

    Android 实现ViewPager边界回弹效果实例代码

    这篇文章主要介绍了Android 实现ViewPager边界回弹效果的实例代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-05-05
  • 简单实现Android滚动公告栏

    简单实现Android滚动公告栏

    这篇文章主要为大家详细介绍了如何简单实现Android滚动公告栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Android性能优化之ViewPagers + Fragment缓存优化

    Android性能优化之ViewPagers + Fragment缓存优化

    这篇文章主要介绍了Android性能优化之ViewPagers+Fragment缓存优化,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • Android开启动画之渐隐渐现效果

    Android开启动画之渐隐渐现效果

    这篇文章主要为大家详细介绍了Android开启动画之渐隐渐现效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Compose开发之动画艺术探索及实现示例

    Compose开发之动画艺术探索及实现示例

    这篇文章主要为大家介绍了Compose开发之动画艺术探索及实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • Android实现带节点的进度条

    Android实现带节点的进度条

    这篇文章主要为大家详细介绍了Android实现带节点的进度条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-03-03
  • Android垃圾回收机制解决内存泄露问题

    Android垃圾回收机制解决内存泄露问题

    这篇文章主要介绍了Android垃圾回收机制解决内存泄露问题的相关资料,需要的朋友可以参考下
    2016-02-02

最新评论