Android之使用Bundle進(jìn)行IPC詳解
一、Bundle進(jìn)行IPC介紹
四大組件中的三大組件(Activity、Service、Receiver)都是支持在Intent中傳遞Bundle數(shù)據(jù)的,由于Bundle實(shí)現(xiàn)了Parcelable接口,所以它可以方便地在不同的進(jìn)程之間傳輸。當(dāng)然,傳輸?shù)臄?shù)據(jù)必須能夠被序列化,比如基本類型、實(shí)現(xiàn)了Parcelable接口的對(duì)象、實(shí)現(xiàn)了Serializable接口的對(duì)象以及一些Android支持的特殊對(duì)象,具體內(nèi)容可以看Bundle這個(gè)類,就可以看到所有它支持的類型。Bundle不支持的類型無法通過它在進(jìn)程間傳遞數(shù)據(jù)。
二、使用方法
1.打包數(shù)據(jù)發(fā)送
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.接受數(shù)據(jù)
Intent intent = getIntent(); Bundle bundle = intent.getExtras(); String name = bundle.getString("name"); int age = bundle.getInt("age");
3.在AndroidManifest.xml中開啟多進(jìn)程
<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
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Android跨進(jìn)程IPC通信AIDL機(jī)制原理
- Android IPC機(jī)制Messenger實(shí)例詳解
- Android系統(tǒng)進(jìn)程間通信(IPC)機(jī)制Binder中的Server和Client獲得Service Manager接口之路
- 淺談Service Manager成為Android進(jìn)程間通信(IPC)機(jī)制Binder守護(hù)進(jìn)程之路
- Android clipChildren屬性實(shí)例詳解
- android IPC之binder通信機(jī)制
- Android IPC機(jī)制ACtivity綁定Service通信代碼實(shí)例
相關(guān)文章
Android網(wǎng)絡(luò)編程之簡易新聞客戶端
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)絡(luò)編程之簡易新聞客戶端的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Android實(shí)現(xiàn)多個(gè)連續(xù)帶數(shù)字圓圈效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多個(gè)連續(xù)帶數(shù)字圓圈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android編程使用Fragment界面向下跳轉(zhuǎn)并一級(jí)級(jí)返回的實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程使用Fragment界面向下跳轉(zhuǎn)并一級(jí)級(jí)返回的實(shí)現(xiàn)方法,較為詳細(xì)的分析了Fragment界面跳轉(zhuǎn)所涉及的相關(guān)知識(shí)點(diǎn)與實(shí)現(xiàn)技巧,并附帶了完整的實(shí)例代碼供讀者下載參考,需要的朋友可以參考下2015-10-10Android實(shí)現(xiàn)CoverFlow效果控件的實(shí)例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)CoverFlow效果控件的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05MVVMLight項(xiàng)目之雙向數(shù)據(jù)綁定
這篇文章主要介紹了MVVMLight項(xiàng)目中雙向數(shù)據(jù)綁定的示例源碼及實(shí)現(xiàn)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步除夕快樂,新年快樂2022-01-01Android編程實(shí)現(xiàn)攝像頭臨摹效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)攝像頭臨摹效果的方法,涉及Android權(quán)限控制、布局及攝像頭功能調(diào)用等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09Android?完整購物商城界面的實(shí)現(xiàn)案例
這篇文章為大家?guī)硪粋€(gè)Android?完整購物商城的界面具體的實(shí)現(xiàn),案例中含有商品列表的顯示,為商城最重要的功能之一,感興趣的朋友來看看吧2022-03-03Android判斷用戶是否允許了攝像頭權(quán)限實(shí)例代碼
本篇文章主要介紹了Android判斷用戶是否允許了攝像頭權(quán)限實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Android 實(shí)現(xiàn)控件懸浮效果實(shí)例代碼
本篇文章主要介紹了Android 實(shí)現(xiàn)控件懸浮效果實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01Android中創(chuàng)建多線程管理器實(shí)例
這篇文章主要介紹了Android中創(chuàng)建多線程管理器實(shí)例,著重講解需要做的哪些事情,每一步都配有代碼例子,需要的朋友可以參考下2014-06-06