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



一.概述

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

           

二.实例

    第一步:新建两个model,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.aidl
package com.test.client.bean;

// Declare any non-default types here with import statements

parcelable Student;
第三步,再新建一个aidl文件,IStudentAidlInterface这个接口有什么用呢?(请看下面)

// IStudentAidlInterface.aidl
package com.test.client.bean;

import com.test.client.bean.Student;

// Declare any non-default types here with import statements

interface 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
千里马8年Android系统及应用开发经验,曾担任过美国unokiwi公司移动端技术总监兼架构师,对系统开发,性能优化,应用高级开发有深入的研究,Android开源定制ROM Lineage的贡献者之一,国内首家线下开辟培训Android Framework课程,拥有2年的Android系统培训经验。成为腾讯课堂专业负责android framework课程分享第一人,致力于提高国内android Framework水平Android Framework领域内是国内各大手机终端科技公司需要的人才,应用开发者都对Android系统充满着好奇,其中的binder是重中之重,都说无binder无Android,binde是Android系统的任督二脉。课程水平循序渐进,由中级再到高级,满足各个层次水平的android开发者。1、灵活使用binder跨进程通信,在app端对它的任何api方法等使用自如2、可以单独分析android系统源码中任何binder部分,分析再也没有难度3、掌握binder驱动本质原理,及对应binder驱动怎么进行跨进程通信,及内存等拷贝方式数据等4、对binder从上层的java app端一直到最底层的内核binder驱动,都可以顺利理通5、针对系统开发过程中遇到的binder报错等分析方法,及binder bug案例学习6、针对面试官任何的binder问题都可以对答自如7、socket这种跨进程通信实战使用8、针对android源码中使用的socket源码轻松掌握9、android系统源码中最常见的socketpair中双向跨进程通信10、使用socket实现一个可以让app执行shell命令的程序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值