AndroidAIDL实现两个APP间的跨进程通信实例
- 格式:pdf
- 大小:73.77 KB
- 文档页数:5
AndroidAIDL实现两个APP间的跨进程通信实例
本⽂为⼤家分享了Android AIDL实现两个APP间的跨进程通信实例,供⼤家参考,具体内容如下
1 Service端创建
⾸先需要创建⼀个Android⼯程然后创建AIDL⽂件,创建AIDL⽂件主要为了⽣成继承了Binder的Stub类,以便应⽤Binder进⾏
进程间通信
servier端结构如下
AIDL代码如下
// IBookManager.aidl
package com.example.bookserver.aidl;
// Declare any non-default types here with import statements
import com.example.bookserver.aidl.Book;
interface IBookManager {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
List getBook();
boolean addBook(in Book book);
}
package com.example.bookserver.aidl;
parcelable Book;
之后创建⼀个实现了Parcelable的Book.java类⽤来传递数据
package com.example.bookserver.aidl;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by SAMSUNG on 2016-09-07.
*/
public class Book implements Parcelable {
private int id;
private String name ;
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.name);
}
public Book() {
}
protected Book(Parcel in) {
this.id = in.readInt();
this.name = in.readString();
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
@Override
public Book createFromParcel(Parcel source) {
return new Book(source);
}
@Override
public Book[] newArray(int size) {
return new Book[size];
}
};
}
最后我们来写⼀个Service⽤于客户端绑定
package com.example.bookserver.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import com.example.bookserver.aidl.Book;
import com.example.bookserver.aidl.IBookManager;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class BookService extends Service {
private CopyOnWriteArrayList boookList = new CopyOnWriteArrayList();
public BookService() {
} Binder binder = new IBookManager.Stub(){
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public List getBook() throws RemoteException {
return boookList;
}
@Override
public boolean addBook(Book book) throws RemoteException {
return boookList.add(book);
}
};
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onCreate() {
super.onCreate();
Book book = new Book();
book.setId(12345);
book.setName("Book 1");
boookList.add(book);
}
}
这样Server端就搞定了,接下来就该进⾏Client端的代码编写了
2 Client端
Client端结构如下
⾸先我们要讲AndroidStudio 通过AIDL⽣成的Binder导⼊到Client中并将Book.java也导⼊到Client中
然后写进⾏Service的绑定
package com.example.bookclient;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.IBinder;
import android.util.Log;
import com.example.bookserver.aidl.IBookManager;
import java.util.List;
/** * Created by SAMSUNG on 2016-09-07.
*/
public class BookServiceManager {
Context mContext = null;
IBookManager mService = null;
private static BookServiceManager bsm ;
public static BookServiceManager getInstance(Context context){
if(bsm==null){
bsm = new BookServiceManager(context);
}
return bsm;
}
public IBookManager getBookServie(){
while (mService==null){
Log.d("BookServiceManager", "getBookServie: ");
this.connectService();
}
return mService;
}
public BookServiceManager(Context mContext) {
this.mContext = mContext;
}
ServiceConnection scc = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("BookServiceManager", "getBookServie: 2 ==> Bind ");
mService = IBookManager.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
};
public boolean connectService(){
if(mService == null){
Log.d("BookServiceManager", "getBookServie: 2");
Intent intent = new Intent("com.example.bookserver.service.BookService");
final Intent eintent = new Intent(createExplicitFromImplicitIntent(mContext,intent));
mContext.bindService(eintent,scc, Service.BIND_AUTO_CREATE);
}
return true;
}
public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
// Retrieve all services that can match the given intent
PackageManager pm = context.getPackageManager();
List resolveInfo = pm.queryIntentServices(implicitIntent, 0);
// Make sure only one match was found