Android之使用Bundle進行IPC

來源:互聯網
上載者:User

標籤:mode   over   介紹   exce   ppt   state   rmi   ibinder   cli   

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

完整代碼:https://github.com/ZhangMiao147/IPCDemo

Android之使用Bundle進行IPC

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.