android多进程通信的几种方式一

来源:互联网 发布:近年来重庆物流数据 编辑:程序博客网 时间:2024/05/01 02:23



一.概述

  说起android中多进程通信,大家首先想到的是aidl,今天我们就来复习一下aidl在多进程之间的通信,根据官方文档,aidl全拼是Android Interface Definition Language,安卓接口定制语言.作用就是在安卓设备中多个进程之间传输数据,说白了就是一个进程调用另外一个进程获取信息的方式.

           

二.实例

   第一步,新建两个model(基于androidStudio上开发),clientproject和serverproject,然后创建一个javabean(两个进程之间用来传递这个对象)

package com.test.client.bean;import android.os.Parcel;import android.os.Parcelable;public class Student implements Parcelable {    public int studentId;    public String name;    public Student(int studentId, String name) {        this.studentId = studentId;        this.name = name;    }    protected Student(Parcel in) {        studentId = in.readInt();        name = in.readString();    }    public static final Creator<Student> CREATOR = new Creator<Student>() {        @Override        public Student createFromParcel(Parcel in) {            return new Student(in);        }        @Override        public Student[] newArray(int size) {            return new Student[size];        }    };    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeInt(studentId);        dest.writeString(name);    }}
第二步,我们新建aidl文件,请注意包名(跟上边的Student的包名一样),有的人可能认为新建aidl的时候命名不能为Student,解决办法就是先随便写一个类名,然后在修改为Student即可

// IStudentAidlInterface.aidlpackage com.test.client.bean;// Declare any non-default types here with import statementsparcelable Student;
第三步,再新建一个aidl文件,IStudentAidlInterface这个接口有什么用呢?(请看下面)

// IStudentAidlInterface.aidlpackage com.test.client.bean;import com.test.client.bean.Student;// Declare any non-default types here with import statementsinterface IStudentAidlInterface {    Student getStudent();}
第四步,写我们的clientproject中的activity,注意下面的service名称是根据serverproject中的server名称来的
package com.test.client;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.view.View;import android.widget.Button;import android.widget.TextView;import com.test.client.bean.IStudentAidlInterface;import com.test.client.bean.Student;public class AIDLActivity extends Activity {    private Button btn_get_data;    private IStudentAidlInterface mIStudentAidlInterface;    private TextView tv_content;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_aidl);        //绑定进程通信的服务        Intent mIntent = new Intent("com.test.server.service.StudentService");        bindService(mIntent, mconn, Context.BIND_AUTO_CREATE);        tv_content = (TextView) findViewById(R.id.tv_content);        btn_get_data = (Button) findViewById(R.id.btn_get_data);        btn_get_data.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (mIStudentAidlInterface != null) {                    try {                        Student mStudent = mIStudentAidlInterface.getStudent();                        tv_content.setText("获取到服务进程的数据是:" + mStudent.studentId + "-------" + mStudent.name);                    } catch (RemoteException e) {                        e.printStackTrace();                    }                }            }        });    }    private ServiceConnection mconn = new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            mIStudentAidlInterface = IStudentAidlInterface.Stub.asInterface(service);        }        @Override        public void onServiceDisconnected(ComponentName name) {            mIStudentAidlInterface = null;        }    };}

至此我们clientproject已经编码完毕,下面编写serverproject

第一步,把clientproject中的aidl文件复制一份到serverproject项目中,并且把Student对象也复制一份到serverproject中包括包名
第二步,编写StudentService
package com.test.server.service;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import android.support.annotation.Nullable;import com.test.client.bean.IStudentAidlInterface;import com.test.client.bean.Student;public class StudentService extends Service {    @Nullable    @Override    public IBinder onBind(Intent intent) {        return mIBinder;    }    IStudentAidlInterface.Stub mIBinder = new IStudentAidlInterface.Stub() {        @Override        public Student getStudent() throws RemoteException {            return new Student(1, "名字是:哈哈哈");        }    };}
最后一定要记得在androidmanifests.xml文件中注册
<service android:name=".service.StudentService">            <intent-filter>                <action android:name="com.test.server.service.StudentService" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </service>

这样就大功搞成了,运行serverproject和clientproject你就能看到效果了
附上demo下载链接   http://download.csdn.net/detail/u010648159/9625027

1 0
原创粉丝点击