Android AIDL實(shí)現(xiàn)兩個(gè)APP間的跨進(jìn)程通信實(shí)例
本文為大家分享了Android AIDL實(shí)現(xiàn)兩個(gè)APP間的跨進(jìn)程通信實(shí)例,供大家參考,具體內(nèi)容如下
1 Service端創(chuàng)建
首先需要?jiǎng)?chuàng)建一個(gè)Android工程然后創(chuàng)建AIDL文件,創(chuàng)建AIDL文件主要為了生成繼承了Binder的Stub類,以便應(yīng)用Binder進(jìn)行進(jìn)程間通信
servier端結(jié)構(gòu)如下

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<Book> getBook();
boolean addBook(in Book book);
}
package com.example.bookserver.aidl; parcelable Book;
之后創(chuàng)建一個(gè)實(shí)現(xiàn)了Parcelable的Book.java類用來傳遞數(shù)據(jù)
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<Book> CREATOR = new Parcelable.Creator<Book>() {
@Override
public Book createFromParcel(Parcel source) {
return new Book(source);
}
@Override
public Book[] newArray(int size) {
return new Book[size];
}
};
}
最后我們來寫一個(gè)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<Book> boookList = new CopyOnWriteArrayList<Book>();
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<Book> 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端就搞定了,接下來就該進(jìn)行Client端的代碼編寫了
2 Client端
Client端結(jié)構(gòu)如下

首先我們要講AndroidStudio 通過AIDL生成的Binder導(dǎo)入到Client中并將Book.java也導(dǎo)入到Client中
然后寫進(jìn)行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> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
// Make sure only one match was found
if (resolveInfo == null || resolveInfo.size() != 1) {
return null;
}
// Get component info and create ComponentName
ResolveInfo serviceInfo = resolveInfo.get(0);
String packageName = serviceInfo.serviceInfo.packageName;
String className = serviceInfo.serviceInfo.name;
ComponentName component = new ComponentName(packageName, className);
// Create a new intent. Use the old one for extras and such reuse
Intent explicitIntent = new Intent(implicitIntent);
// Set the component to be explicit
explicitIntent.setComponent(component);
return explicitIntent;
}
}
最后對(duì)設(shè)置Button進(jìn)行調(diào)用
package com.example.bookclient;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.example.bookserver.aidl.Book;
import com.example.bookserver.aidl.IBookManager;
public class MainActivity extends AppCompatActivity {
IBookManager mBookService ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
Button addButton = (Button) findViewById(R.id.button3);
Button findButton = (Button) findViewById(R.id.button2);
BookServiceManager.getInstance(getApplication()).connectService();
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
mBookService = BookServiceManager.getInstance(getApplication()).getBookServie();
}
});
addButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Book book = new Book();
book.setId(2345);
book.setName("add book!!");
try {
mBookService.addBook(book);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
findButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
try {
Log.d("MainActivity", mBookService.getBook().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
}
這樣我們就實(shí)現(xiàn)了AIDL的不同APP的調(diào)用。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程實(shí)現(xiàn)識(shí)別與掛載U盤的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)識(shí)別與掛載U盤的方法,對(duì)比分析了Android針對(duì)U盤的識(shí)別與掛載技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-02-02
Android實(shí)現(xiàn)強(qiáng)制下線功能的示例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)強(qiáng)制下線功能的示例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Android UI實(shí)現(xiàn)多行文本折疊展開效果
這篇文章主要為大家詳細(xì)介紹了Android UI實(shí)現(xiàn)多行文本折疊展開效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android 帶箭頭的指引tipLayout實(shí)現(xiàn)示例代碼
本篇文章主要介紹了Android 帶箭頭的指引tipLayout實(shí)現(xiàn)示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
Android ListView 和ScroolView 出現(xiàn)onmeasure空指針的解決辦法
這篇文章主要介紹了Android ListView 和ScroolView 出現(xiàn)onmeasure空指針的解決辦法的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android仿微博個(gè)人詳情頁滾動(dòng)到頂部的實(shí)例代碼
這篇文章主要介紹了Android仿微博個(gè)人詳情頁滾動(dòng)到頂部的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒家,需要的朋友可以參考下2019-05-05
Android音頻編輯之音頻轉(zhuǎn)換PCM與WAV
這篇文章主要為大家詳細(xì)介紹了Android音頻編輯之音頻轉(zhuǎn)換PCM與WAV,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Android自動(dòng)測(cè)試工具M(jìn)onkey的實(shí)現(xiàn)方法
本文主要介紹Android Monkey 實(shí)現(xiàn)方法,Monkey測(cè)試是一種為了測(cè)試軟件的穩(wěn)定性、健壯性的快速有效的方法,具有非常重要的參考價(jià)值,希望對(duì)小伙伴有所幫助2016-07-07
如何為RecyclerView添加Header和Footer
這篇文章主要為大家詳細(xì)介紹了如何為RecyclerView添加Header和Footer,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12

