iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android之使用Bundle进行IPC详解
  • 531
分享到

Android之使用Bundle进行IPC详解

ipcbundleAndroid 2022-06-06 05:06:15 531人浏览 独家记忆
摘要

一、Bundle进行IPC介绍 四大组件中的三大组件(Activity、Service、Receiver)都是支持在Intent中传递Bundle数据的,由于Bundle实现

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

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使用文件进行IPCAndroid IPC进程间通信详解最新AndroidStudio的AIDL操作)Android系统进程间通信(IPC)机制Binder中的Client获得Server远程接口过程源代码分析Android系统进程间通信(IPC)机制Binder中的Server启动过程源代码分析Android系统进程间通信(IPC)机制Binder中的Server和Client获得Service Manager接口之路浅谈Service Manager成为Android进程间通信(IPC)机制Binder守护进程之路Android进程间通信(IPC)机制Binder简要介绍Android clipChildren属性实例详解Android IPC机制利用Messenger实现跨进程通信Android IPC机制绑定Service实现本地通信android IPC之binder通信机制


--结束END--

本文标题: Android之使用Bundle进行IPC详解

本文链接: https://www.lsjlt.com/news/22782.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作