I. Bundle for IPC INTRODUCTION
The three components of the four components (Activity, Service, Receiver) are supported to pass the bundle data in the intent, because the bundle implements the Parcelable interface, so it can easily transfer between different processes. Of course, the transmitted data must be able to be serialized, such as the basic type, the object that implements the Parcelable interface, the object that implements the serializable interface, and some special objects that are supported by Android, the specific content can see the bundle this class, You can see all the types it supports. A type that is not supported by a bundle cannot pass data between processes through it.
Ii. methods of Use 1. Package Data Sending
New Intent (mainactivity. this, thirdactivity. class New Bundle (); Bundle.putcharsequence ("name", "Zhangmiao"); Bundle.putint ("Age" ,Intent1.putextras (bundle); StartActivity (intent1) ;
2. Acceptance of data
Intent Intent == = bundle.getstring ("name"); int age = bundle.getint ("Age");
3. Open multi-process in Androidmanifest.xml
< Activity ... android:process= ": Remote"/>
Three, small case 1. Modify the Activity_main.xml file
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns: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" > <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_horizontal"Android:text= "Bundler"> </TextView> <ButtonAndroid:id= "@+id/bundler_button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Send Message"> </Button></LinearLayout>
2. Add a Activity_third.xml file
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_horizontal"Android:text= "at activity third" /> <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Activity Third" /></LinearLayout>
3. Add Thirdactivity Class
PackageCom.zhangmiao.ipcdemo;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.widget.TextView;/*** Created by Zhangmiao on 2016/12/27.*/ Public classThirdactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_third); Intent Intent=getintent (); Bundle Bundle=Intent.getextras (); String name= bundle.getstring ("name"); intAge = Bundle.getint ("Age"); TextView TextView=(TextView) Findviewbyid (R.ID.TEXTVIEW1); Textview.settext ("Name:" + name + ", Age:" +Age ); }}4. Modify the Mainactivity class
PackageCom.zhangmiao.ipcdemo;ImportAndroid.content.ComponentName;ImportAndroid.content.Context;Importandroid.content.Intent;Importandroid.content.ServiceConnection;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.IBinder;ImportAndroid.os.Message;ImportAndroid.os.Messenger;Importandroid.os.RemoteException;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.ObjectOutputStream;Importjava.io.Serializable; Public classMainactivityextendsappcompatactivity {Private Static FinalString TAG = "Mainactivity";@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main);button Button=(Button) Findviewbyid (R.id.bundler_button); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent intent1=NewIntent (mainactivity. This, Thirdactivity.class); Bundle Bundle=NewBundle (); 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"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.zhangmiao.ipcdemo"> <uses-permissionAndroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE" /> <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme"> <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name"Android:launchmode= "Standard"Android:theme= "@style/apptheme.noactionbar"> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> <ActivityAndroid:name=". Thirdactivity "android:configchanges= "Screenlayout"Android:label= "@string/app_name"android:process= ": Remote" /> </Application></Manifest>
Complete code: Https://github.com/ZhangMiao147/IPCDemo
Android uses bundles for IPC