android AIDL示例代码(mark下)
生活随笔
收集整理的這篇文章主要介紹了
android AIDL示例代码(mark下)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.demo結構圖
?
2.ipcclient
Book類。
package com.mu.guoxw.ipcclient;import android.os.Parcel; import android.os.Parcelable;/*** Created by guoxw on 2018/3/21.*/public class Book implements Parcelable {public String getName() {return name;}public void setName(String name) {this.name = name;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}private String name;private int price;public Book(){}public Book(Parcel in) {name = in.readString();price = in.readInt();}public static final Creator<Book> CREATOR = new Creator<Book>() {@Overridepublic Book createFromParcel(Parcel in) {return new Book(in);}@Overridepublic Book[] newArray(int size) {return new Book[size];}};@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeString(name);dest.writeInt(price);}/*** 參數是一個Parcel,用它來存儲與傳輸數據* @param dest*/public void readFromParcel(Parcel dest) {//注意,此處的讀值順序應當是和writeToParcel()方法中一致的name = dest.readString();price = dest.readInt();}//方便打印數據@Overridepublic String toString() {return "name : " + name + " , price : " + price;} }Book.aidl
// Book.aidl package com.mu.guoxw.ipcclient;// Declare any non-default types here with import statements parcelable Book;BookManager.aidl
package com.mu.guoxw.ipcclient;import com.mu.guoxw.ipcclient.Book; // Declare any non-default types here with import statementsinterface BookManager {/*** Demonstrates some basic types that you can use as parameters* and return values in AIDL.*/List<Book>getBooks();void addBook(inout Book book);Book getBook(); }MainActivity
package com.mu.guoxw.ipcclient;import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;import java.util.List;public class MainActivity extends AppCompatActivity {private BookManager mBookManager = null;private boolean mBound = false;//包含Book對象的listprivate List<Book> mBooks;Button btn_add;TextView text;private ServiceConnection mServiceConnection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {mBookManager = BookManager.Stub.asInterface(service);mBound = true;if (mBookManager != null) {try {mBooks = mBookManager.getBooks();} catch (RemoteException e) {e.printStackTrace();}}}@Overridepublic void onServiceDisconnected(ComponentName name) {Log.e(getLocalClassName(), "service disconnected");mBound = false;}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn_add=findViewById(R.id.btn_add);text=findViewById(R.id.text);btn_add.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {addBook();}});}/*** 按鈕的點擊事件,點擊之后調用服務端的addBookIn方法**/public void addBook() {if (!mBound) {attemptToBindService();Toast.makeText(this, "當前與服務端處于未連接狀態,正在嘗試重連,請稍后再試", Toast.LENGTH_SHORT).show();return;}if (mBookManager == null) return;Book book = new Book();book.setName("測試app_A");book.setPrice(30);try {mBookManager.addBook(book);Log.e("aidl:","client端_添加數據"+ book.toString());List<Book> mbooks=mBookManager.getBooks();for (int i=0;i<mbooks.size();i++){if(mbooks.get(i)!=null){Log.e("aidl:", "addBook: cleint端_打印:"+mbooks.get(i).toString() );}}} catch (RemoteException e) {e.printStackTrace();}}/*** 嘗試與服務端建立連接*/private void attemptToBindService() {Intent intent = new Intent();intent.setAction("com.multak.guoxw.ipcservertest.service.aidl");intent.setPackage("com.multak.guoxw.ipcservertest");bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);}@Overrideprotected void onStart() {super.onStart();if (!mBound) {attemptToBindService();}}@Overrideprotected void onDestroy() {super.onDestroy();super.onStop();if (mBound) {unbindService(mServiceConnection);mBound = false;}} }3:ipcServerTest
? ? Book.aidl
package com.mu.guoxw.ipcclient; import com.mu.guoxw.ipcclien.Book; // Declare any non-default types here with import statements parcelable Book;BookManager.aidl
// BookManager.aidl package com.mu.guoxw.ipcclient; import com.mu.guoxw.ipcclient.Book;interface BookManager {List<Book> getBooks();void addBook(inout Book book);Book getBook(); }
AIDLService
package com.mu.guoxw.ipcservertest.service;import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.support.annotation.Nullable; import android.util.Log;import com.mu.guoxw.ipcclient.Book; import com.mu.guoxw.ipcclient.BookManager;import java.util.ArrayList; import java.util.List;public class AIDLService extends Service {private List<Book> mBooks=new ArrayList<>();public AIDLService() {}private final BookManager.Stub bookManager=new BookManager.Stub() {@Overridepublic List<Book> getBooks() throws RemoteException {synchronized (this){if (mBooks != null) {return mBooks;}return new ArrayList<>();}}@Overridepublic void addBook(Book book) throws RemoteException {synchronized (this){if (mBooks == null) {mBooks = new ArrayList<>();}if (book == null) {book = new Book();}book.setPrice(2333);Log.e("aidl:", " service端_修改數據 "+book.toString());if (!mBooks.contains(book)) {mBooks.add(book);}}}@Overridepublic Book getBook() throws RemoteException {return null;}};@Overridepublic void onCreate() {Book book = new Book();book.setName("Android開發藝術探索");book.setPrice(28);mBooks.add(book);super.onCreate();}@Nullable@Overridepublic IBinder onBind(Intent intent) {Log.e(getClass().getSimpleName(), String.format("on bind,intent = %s", intent.toString()));return bookManager;} }MessengerService[沒用] package com.mu.guoxw.ipcservertest.service;import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.Messenger; import android.support.annotation.Nullable;public class MessengerService extends Service {private static final int SAY_HOLLE = 0;static class ServiceHandler extends Handler {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case SAY_HOLLE:break;default:super.handleMessage(msg);break;}}}final Messenger mMessenger = new Messenger(new ServiceHandler());@Nullable@Overridepublic IBinder onBind(Intent intent) {System.out.println("binding");return mMessenger.getBinder();} }
Book
package com.mu.guoxw.ipcclient;import android.os.Parcel; import android.os.Parcelable;/*** Created by guoxw on 2018/3/21.*/public class Book implements Parcelable {public String getName() {return name;}public void setName(String name) {this.name = name;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}private String name;private int price;public Book() {}protected Book(Parcel in) {name = in.readString();price = in.readInt();}public static Creator<Book> getCREATOR() {return CREATOR;}public static final Creator<Book> CREATOR = new Creator<Book>() {@Overridepublic Book createFromParcel(Parcel in) {return new Book(in);}@Overridepublic Book[] newArray(int size) {return new Book[size];}};@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeString(name);dest.writeInt(price);}public void readFromParcel(Parcel dest) {name = dest.readString();price = dest.readInt();}//方便打印數據@Overridepublic String toString() {return "name : " + name + " , price : " + price;} }4.注冊
<serviceandroid:name=".service.AIDLService"android:enabled="true"android:exported="true" ><intent-filter><action android:name="com.multak.guoxw.ipcservertest.service.aidl"/><category android:name="android.intent.category.DEFAULT"/></intent-filter></service><serviceandroid:name=".service.MessengerService"android:exported="true"><intent-filter><action android:name="com.lypeer.messenger"/><category android:name="android.intent.category.DEFAULT"/></intent-filter></service>
?5.code:AIDL_demo
鏈接: https://pan.baidu.com/s/1ExGhr3U9MAgqR_kpBeGqCQ
?
?
轉載于:https://www.cnblogs.com/galibujianbusana/p/8625856.html
總結
以上是生活随笔為你收集整理的android AIDL示例代码(mark下)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 中resources管理
- 下一篇: Cookie、Session 和 Tok