Android AIDL实现两个APP间的跨进程通信实例
脚本之家 / 编程助手:解决程序员“几乎”所有问题!
脚本之家官方知识库 → 点击立即使用
本文为大家分享了Android AIDL实现两个APP间的跨进程通信实例,供大家参考,具体内容如下
1 Service端创建
首先需要创建一个Android工程然后创建AIDL文件,创建AIDL文件主要为了生成继承了Binder的Stub类,以便应用Binder进行进程间通信
servier端结构如下
AIDL代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // 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); } |
之后创建一个实现了Parcelable的Book.java类用来传递数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | 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用于客户端绑定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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端就搞定了,接下来就该进行Client端的代码编写了
2 Client端
Client端结构如下
首先我们要讲AndroidStudio 通过AIDL生成的Binder导入到Client中并将Book.java也导入到Client中
然后写进行Service的绑定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | 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进行调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | 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的调用。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
Android ListView 和ScroolView 出现onmeasure空指针的解决办法
这篇文章主要介绍了Android ListView 和ScroolView 出现onmeasure空指针的解决办法的相关资料,需要的朋友可以参考下2017-02-02如何为RecyclerView添加Header和Footer
这篇文章主要为大家详细介绍了如何为RecyclerView添加Header和Footer,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2016-12-12
最新评论