Android之使用Bundle进行IPC详解

作者:zhangmiao14 时间:2023-09-27 22:44:56 

一、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>

来源:http://www.cnblogs.com/zhangmiao14/p/6226543.html

标签:ipc,Android
0
投稿

猜你喜欢

  • Android实现将已发送的短信写入短信数据库的方法

    2021-08-20 05:40:57
  • Kotlin中的对象表达式和对象声明的具体使用

    2022-05-31 04:08:18
  • java从输入流中获取数据并返回字节数组示例

    2021-12-08 22:47:36
  • JavaFX Metro UI 和 开发库使用简介

    2021-12-11 19:01:39
  • java冷知识:javac AbstractProcessor详解

    2022-08-01 19:32:09
  • java 创建线程的几种方式

    2023-10-29 19:40:16
  • java连接MySQl数据库实例代码

    2021-12-18 18:19:36
  • mybatisPlus条件构造器常用方法小结

    2023-12-16 07:04:09
  • Android性能优化之plt hook与native线程监控详解

    2021-09-10 22:06:01
  • Log4Net 日志配置[附带源码下载]

    2023-11-10 04:21:22
  • Struts2学习笔记(5)-参数传递方法

    2023-08-30 13:03:21
  • Unity3D实现飞机大战游戏(2)

    2021-11-16 10:41:38
  • Java使用RedisTemplate模糊删除key操作

    2023-06-24 06:45:25
  • Java中的传值与传引用实现过程解析

    2023-02-21 12:59:41
  • Android四大组件之Activity详解

    2022-08-22 00:00:49
  • Android实现界面跳转功能

    2022-05-07 21:51:32
  • C#实现发送邮件的三种方法

    2021-08-06 07:36:00
  • Java回调函数实例代码详解

    2023-11-23 18:22:26
  • Java对zip,rar,7z文件带密码解压实例详解

    2023-11-29 05:08:32
  • Java超详细讲解设计模式中的命令模式

    2023-07-26 15:23:11
  • asp之家 软件编程 m.aspxhome.com