Android AIDL实现两个APP间的跨进程通信实例

Android AIDL实现两个APP间的跨进程通信实例,第1张

概述本文为大家分享了AndroidAIDL实现两个APP间的跨进程通信实例,供大家参考,具体内容如下

本文为大家分享了AndroID AIDL实现两个APP间的跨进程通信实例,供大家参考,具体内容如下

1 Service端创建

首先需要创建一个AndroID工程然后创建AIDL文件,创建AIDL文件主要为了生成继承了Binder的Stub类,以便应用Binder进行进程间通信

servIEr端结构如下

AIDL代码如下

// IBookManager.aIDlpackage com.example.bookserver.aIDl;// Declare any non-default types here with import statementsimport 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;

之后创建一个实现了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<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];  } };}

最后我们来写一个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,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端就搞定了,接下来就该进行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> 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; }}

最后对设置button进行调用

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();    }   }  }); }}

这样我们就实现了AIDL的不同APP的调用。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

您可能感兴趣的文章:Android使用AIDL实现两个App间通信Android AIDL和远程Service调用示例代码Android应用程序四大组件之使用AIDL如何实现跨进程调用ServiceAndroid通过AIDL在两个APP之间Service通信 总结

以上是内存溢出为你收集整理的Android AIDL实现两个APP间的跨进程通信实例全部内容,希望文章能够帮你解决Android AIDL实现两个APP间的跨进程通信实例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1142677.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存