AIDL的实例,看一看结构
16lz
2021-01-24
Android支持两种类型的服务:本地服务与远程服务。本地服务只能供承载它的应用程序使用,而远程服务还可以供其它应用程序使用。在Android中,远程服务可以使用AIDL(Android Interface Definition Language)向客户端定义自身。即Android中IPC机制实现的一种方式,下面看看Android studio中使用aidl的方式。
这个是同一个项目中通过更改清单文件让service跟此项目处于不同的进程。
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="yinwei.com.aidlservice" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!--android:process=":remote"处于不同的进程--> <service android:name=".BookMangerService" android:process=":remote" > <intent-filter> <action android:name="com.yinwei.bookMangerService"/> </intent-filter> </service> </application></manifest>首先是book类:
public class Book implements Parcelable { private String name; private String author; public Book(String name, String author) { this.name = name; this.author = author; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Book() { } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(this.name); dest.writeString(this.author); } protected Book(Parcel in) { this.name = in.readString(); this.author = in.readString(); } public static final Creator<Book> CREATOR = new Creator<Book>() { public Book createFromParcel(Parcel source) { return new Book(source); } public Book[] newArray(int size) { return new Book[size]; } };}
其次是三个aidl文件
// Book.aidlpackage yinwei.com.aidlservice;// 用到的类声明为parcelableparcelable Book;
// onNewBookAdd.aidlpackage yinwei.com.aidlservice;// Declare any non-default types here with import statementsimport yinwei.com.aidlservice.Book;interface onNewBookAddListener { void onNewBookAdd(in Book book);}
// BookManger.aidlpackage yinwei.com.aidlservice;// Declare any non-default types here with import statementsimport yinwei.com.aidlservice.Book;import yinwei.com.aidlservice.onNewBookAddListener;interface BookManger { List<Book> getBookList(); void addBook(in Book book);void registerListener(onNewBookAddListener listener);void unRegisterListener(onNewBookAddListener listener);}
然后搞定我们的service
public class BookMangerService extends Service { //java并发集合 private CopyOnWriteArrayList<Book> bookList=new CopyOnWriteArrayList<>(); //进程操作专用集合 private RemoteCallbackList<onNewBookAddListener> remoteCallbackList=new RemoteCallbackList<>(); //原子操作 private AtomicBoolean mIsDeid=new AtomicBoolean(false); @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. return iBinder; } private final IBinder iBinder=new BookManger.Stub(){ @Override public IBinder asBinder() { return super.asBinder(); } @Override public void addBook(Book book) throws RemoteException { Log.d("book-------", book.getName() + ":" + book.getAuthor()); onNewBookAdd(book); } @Override public List<Book> getBookList() throws RemoteException { return bookList; } @Override public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { return super.onTransact(code, data, reply, flags); } @Override public void unRegisterListener(onNewBookAddListener listener) throws RemoteException { remoteCallbackList.unregister(listener); } @Override public void registerListener(onNewBookAddListener listener) throws RemoteException { remoteCallbackList.register(listener); } }; private void onNewBookAdd(Book book){ bookList.add(book); int n=remoteCallbackList.beginBroadcast(); for (int i=0;i<n;i++){ onNewBookAddListener listener=remoteCallbackList.getBroadcastItem(i); if(listener!=null){ try { listener.onNewBookAdd(book); } catch (RemoteException e) { e.printStackTrace(); } } } remoteCallbackList.finishBroadcast(); } @Override public void onDestroy() { super.onDestroy(); mIsDeid.set(true); }}
public class MainActivity extends AppCompatActivity implements IBinder.DeathRecipient { BookManger bookManger; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //android5.0之后不能使用隐式调用,下面是谷歌推荐的方式 Intent intent=new Intent(); intent.setAction("com.yinwei.bookMangerService"); intent.setPackage(getPackageName()); bindService(intent, serviceConnection, BIND_AUTO_CREATE); } @Override public void binderDied() { } ServiceConnection serviceConnection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { bookManger=BookManger.Stub.asInterface(service); try { service.linkToDeath(MainActivity.this,IBinder.DUMP_TRANSACTION);//绑定binder死亡监听 bookManger.addBook(new Book("Android","yinwei")); bookManger.registerListener(onNewBookAddListener); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { bookManger=null; } }; private onNewBookAddListener onNewBookAddListener=new onNewBookAddListener.Stub(){ @Override public void onNewBookAdd(Book book) throws RemoteException { Log.d("新书book-------", book.getName() + ":" + book.getAuthor()); } }; @Override protected void onDestroy() { super.onDestroy(); try { if(bookManger!=null) { bookManger.unRegisterListener(onNewBookAddListener); bookManger.asBinder().unlinkToDeath(this,IBinder.DUMP_TRANSACTION); } } catch (RemoteException e) { e.printStackTrace(); } }}
同一个项目中aidl的结构比较好处理,我们看看不同项目的
首先我们直接复制过来aidl所属的文件夹,其次是处理book,需要放到相同的包下。
然后绑定我们的远程service
public class MainActivity extends AppCompatActivity { BookManger bookManger; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent=new Intent(); intent.setAction("com.yinwei.bookMangerService"); intent.setPackage("yinwei.com.aidlservice"); bindService(intent,connection,BIND_AUTO_CREATE); } ServiceConnection connection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { bookManger=BookManger.Stub.asInterface(service); try { bookManger.addBook(new Book("九星天辰诀","蜗牛")); bookManger.registerListener(onNewBookAddListener); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { bookManger=null; } }; @Override protected void onDestroy() { super.onDestroy(); unbindService(connection); try { if(bookManger!=null) { bookManger.unRegisterListener(onNewBookAddListener); } } catch (RemoteException e) { e.printStackTrace(); } } private yinwei.com.aidlservice.onNewBookAddListener onNewBookAddListener=new onNewBookAddListener.Stub(){ @Override public void onNewBookAdd(Book book) throws RemoteException { Log.d("新书book-------", book.getName() + ":" + book.getAuthor()); } };}
附上示例下载地址: aidl代码
更多相关文章
- ndk完整编译cocos2dx项目总结
- Android核心分析之二十一Android应用框架之AndroidApplication
- Jenkins构建时’Users/Mac/Library/Android/sdk’ does not exis
- Android之MVP 模式:简单易懂的介绍方式
- android r cannot be resolved to a variable 错误以及 所有的文
- Android(安卓)Studio导入SlidingMenu类库的方法(其他类库应该也适
- android 应用程序Activity之间数据传递与共享的几种途径
- Android开发秘籍学习笔记(八)
- 【译】Android(安卓)Bluetooth