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中包括包名
第二步,编写StudentServicepackage 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
- android多进程通信的几种方式一
- android多进程的几种通信方式二
- Android中进程间通信的几种方式
- Android 进程间通信的几种实现方式
- Android进程间通信的几种方式
- Android--进程间通信的几种实现方式
- Android--进程间通信的几种实现方式
- Android进程间通信的几种方式
- Android进程间通信的几种方式
- 详解Android 进程间通信的几种实现方式
- 【进程间通信】Android 进程间通信的几种实现方式
- win进程通信的几种方式
- 进程间通信的几种方式
- windows进程通信的几种方式
- linux进程通信的几种方式
- 几种进程间通信的方式
- Windows 进程通信的几种方式
- 几种进程间的通信方式
- 二分查找或插入
- android中确定键重复点击,却只想在时间内,只显示点击一次
- 158.Two Strings Are Anagrams-两个字符串是变位词(容易题)
- Appium 基础概念
- 新开始,java基础小结
- android多进程通信的几种方式一
- Appium - WebView测试(Android)
- PAT How Long Does It Take (25) (拓扑排序)
- 原串翻转
- 数据结构学习记录-1
- 全局变量赋值问题
- 链表反转C++
- 指针中星号的位置问题
- java的System.getProperty()方法详解