Android: Using Bundle for IPC and androidbundleipc
I. Introduction of Bundle to IPC
The three components (Activity, Service, and consumer ER) of the four major components support the transfer of Bundle data in Intent. Because Bundle implements the Parcelable interface, therefore, it can be easily transmitted between different processes. Of course, the transmitted data must be serialized, such as basic types, objects that implement the Parcelable interface, objects that implement the Serializable interface, and some special objects supported by Android, for details, you can see the Bundle class to see all the types it supports. Bundle does not support data transmission between processes.
Ii. Usage 1. Package data for sending
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. Accept data
Intent intent = getIntent();Bundle bundle = intent.getExtras();String name = bundle.getString("name");int age = bundle.getInt("age");3. Enable multi-process in AndroidManifest. xml
<activity ... android:process=":remote" />
Iii. Case 1. Modify the activity_main.xml File
<?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. Add the activity_third.xml File
<?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. Add ThirdActivity class
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. Modify the MainActivity class
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. Modify the AndroidManifest. xml file
<?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>
Full: https://github.com/ZhangMiao147/IPCDemo