本文為Android開發(fā)藝術(shù)探索的筆記榕酒,僅供學(xué)習(xí)
4.4 AIDL的使用
前面Messenger進(jìn)程通信中,如果客戶端有大量的消息需要發(fā)送到服務(wù)端,那么服務(wù)端也只能一個(gè)個(gè)處理烈涮,所以在處理大數(shù)據(jù)的時(shí)候使用Messenger并不是好方法。我們可以使用AIDL來實(shí)現(xiàn)跨進(jìn)程窖剑,所以Messenger的底層是AIDL換句話說Messenger就是AIDL坚洽,只不過系統(tǒng)做了封裝方便我們使用。有了Binder的基礎(chǔ)我們可以更好的理解AIDL西土。
服務(wù)端要?jiǎng)?chuàng)建一個(gè)Service去監(jiān)聽客戶端發(fā)來的消息讶舰,然后建立一個(gè)AIDL的文件夾,將暴露給客戶端的接口在AIDL文件夾里聲明需了,最后在Service去實(shí)現(xiàn)這個(gè)AIDL即可跳昼。
客戶端,需要綁帶Service肋乍,綁帶成功后把服務(wù)端返回的Binder轉(zhuǎn)化為AIDL所屬的類型鹅颊,接著調(diào)用AIDL里的方法即可。
我們來舉個(gè)例子住拭,大致的業(yè)務(wù)邏輯就是主要有三個(gè)功能挪略,1.客戶端可以向服務(wù)端添加Book 2.客戶端可以向服務(wù)端獲取Book信息 3.向服務(wù)端添加Book監(jiān)聽,監(jiān)聽每次添加新Book的信息
下面上代碼
AIDL
// Book.aidl
package com.example.gyh.myapplication;
// Declare any non-default types here with import statements
parcelable Book;
該類主要就是用來聲明Book這個(gè)Bean
// IOnNewBookArrivedListener.aidl
package com.example.gyh.myapplication;
// Declare any non-default types here with import statements
import com.example.gyh.myapplication.Book;
interface IOnNewBookArrivedListener {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void onNewBookArrived(in Book newBook);
}
// IBookManager.aidl
package com.example.gyh.myapplication;
// Declare any non-default types here with import statements
import com.example.gyh.myapplication.Book;
import com.example.gyh.myapplication.IOnNewBookArrivedListener;
interface IBookManager {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void addBook(in Book book);
List<Book> getBookList();
void registerListener(IOnNewBookArrivedListener listener);
void unregisterListener(IOnNewBookArrivedListener listener);
接下來附上服務(wù)端的代碼,正如前面Binder機(jī)制一樣滔岳,創(chuàng)建AIDL業(yè)務(wù)的接口IBookManager.Stub這是運(yùn)行在客戶端的杠娱,然后就會(huì)自動(dòng)生成方法。
public class ServiceBook extends Service {
private CopyOnWriteArrayList<Book> mBookList = new CopyOnWriteArrayList<Book>();//CopyOnWriteArrayList支持并發(fā)的讀寫
// private CopyOnWriteArrayList<IOnNewBookArrivedListener> mlListeners = new CopyOnWriteArrayList<IOnNewBookArrivedListener>();不支持多進(jìn)程對(duì)Listener的增刪
private RemoteCallbackList<IOnNewBookArrivedListener> mListenerList = new RemoteCallbackList<IOnNewBookArrivedListener>();//為什么要用這方式的List谱煤?因?yàn)镽emoteCallbackList支持多進(jìn)程對(duì)Listener的增刪
String TAG = "Service";
boolean isadd = true;
private Binder binder = new IBookManager.Stub() {//里面的方法和AIDL接口一一對(duì)應(yīng)
@Override
public void addBook(Book book) throws RemoteException {
mBookList.add(book);
}
@Override
public List<Book> getBookList() throws RemoteException {
return mBookList;
}
@Override
public void registerListener(IOnNewBookArrivedListener listener) throws RemoteException {
// if (!mlListeners.contains(listener)) {
// mlListeners.add(listener);
// } else {
// Log.i(TAG, "have the same");
// }
mListenerList.register(listener);
Log.i(TAG, "register size" + mListenerList.beginBroadcast());
mListenerList.finishBroadcast();//每一次執(zhí)行完都要finish一下
}
@Override
public void unregisterListener(IOnNewBookArrivedListener listener) throws RemoteException {
// if (mlListeners.contains(listener)) {
// mlListeners.remove(listener);
// } else {
// Log.i(TAG, "no find");
// }
mListenerList.unregister(listener);
Log.i(TAG, "unregister size " + mListenerList.beginBroadcast());
mListenerList.finishBroadcast();
}
};
@Override
public void onCreate() {
super.onCreate();
mBookList.add(new Book(1, "ios"));
mBookList.add(new Book(2, "android"));
new Thread(new ServiceWorker()).start();//定義一個(gè)線程摊求,每隔兩秒去增加一個(gè)Book,目的是為了驗(yàn)證監(jiān)聽是否成功
}
@Override
public void onDestroy() {
isadd = false;
super.onDestroy();
}
void addNewbook(Book book) throws RemoteException {
mBookList.add(book);
final int N = mListenerList.beginBroadcast();
for (int i = 0; i < N; i++) {
IOnNewBookArrivedListener l = mListenerList.getBroadcastItem(i);
if (l != null) {
try {
l.onNewBookArrived(book);//給這個(gè)回掉賦值刘离,以并于客戶端在使用時(shí)有返回值
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
mListenerList.finishBroadcast();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return binder;
}
private class ServiceWorker implements Runnable {
@Override
public void run() {
while (isadd) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int id = mBookList.size() + 1;
String name = mBookList.size() + 1 + "";
try {
addNewbook(new Book(id, name));
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
}
}
下面是客戶端的代碼
通過服務(wù)端返回的Binder對(duì)于也就是 IBookManager.Stub()對(duì)象室叉,里面的asInterface方法可以返回對(duì)于的AIDL接口,沒印象的可以看看IPC機(jī)制詳解(1),從而去調(diào)用相應(yīng)的方法
public class Main2Activity extends AppCompatActivity {
String TAG = "Main2";
IBookManager manager;
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
IBookManager iBookManager = IBookManager.Stub.asInterface(service);/通過這個(gè)Binder去使用服務(wù)端的方法
manager = iBookManager;
List<Book> list = new ArrayList<>();
try {
list = iBookManager.getBookList();//服務(wù)端的方法
Log.i(TAG, list.size() + " " + list.get(0).getName());
iBookManager.addBook(new Book(3, "nihao"));//服務(wù)端的方法
Log.i(TAG, list.size() + " " + list.get(list.size() - 1).getName());
iBookManager.registerListener(listener);//服務(wù)端的方法
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
IOnNewBookArrivedListener listener = new IOnNewBookArrivedListener.Stub() {
@Override
public void onNewBookArrived(Book newBook) throws RemoteException {
if (newBook != null) {
Log.i(TAG, "Add new Book" + newBook.getId() + " " + newBook.getName());//因?yàn)樵诜?wù)端的Addnewbook的方法里添加了回掉的數(shù)據(jù)硫惕,所以newBook是有值得
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = new Intent(Main2Activity.this, ServiceBook.class);
bindService(intent, connection, BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
Log.i(TAG, "Destory");
if (manager != null && manager.asBinder().isBinderAlive()) {
try {
Log.i(TAG, "unregister activity" + manager);
manager.unregisterListener(listener);
} catch (RemoteException e) {
e.printStackTrace();
}
}//當(dāng)Activity銷毀的時(shí)候茧痕,去接觸注冊(cè)
unbindService(connection);
super.onDestroy();
}
}
4.5 ContentProvider的使用
ContentProvider在Android中專門用于不同App之間的數(shù)據(jù)共享的,由此可見ContentProvider天生就可以用來實(shí)現(xiàn)跨進(jìn)程通信恼除。ContentProvider的底層也是用到了Binder踪旷,可見Binder在Android系統(tǒng)中是多么的重要曼氛。雖然ContentProvider的底層是Binder,但是系統(tǒng)已經(jīng)為我們封裝好了令野,使用起來也比AIDL要簡(jiǎn)單的多舀患。
那么我們就來自定義個(gè)ContentProvider,首先去建一個(gè)類叫BookProvider 繼承ContentProvider气破,
public class BookProvider extends ContentProvider {
@Override
//可以進(jìn)行一些初始化 該方法運(yùn)行在主線程里聊浅,其他五個(gè)方法運(yùn)行在Binder線程池里
public boolean onCreate() {
return false;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return null;
}
@Nullable
@Override
//用于返回Uri請(qǐng)求對(duì)于的MIME類型(媒體類型)
public String getType(Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
}
接著我們要去注冊(cè)ContentProvider
<provider
android:name=".ContentProvider_text.BookProvider"
android:authorities="com.example.gyh.myapplication.ContentProvider_text.BookProvider"http://是ContentProvider的唯一標(biāo)識(shí)
android:permission="com.example.gyh.Provider"http://訪問權(quán)限
android:process=":provider"http://ContentProvider運(yùn)行在單獨(dú)的進(jìn)程中
android:readPermission="com.example.gyh.Provider.read"http://讀取權(quán)限
android:writePermission="com.example.gyh.Provider.write" //寫權(quán)限 />
建一個(gè)Activity
Uri uri = Uri.parse("content://com.example.gyh.myapplication.ContentProvider_text.BookProvider");這就是xml里生命的唯一標(biāo)識(shí)符
getContentResolver().query(uri, null, null, null, null);
getContentResolver().query(uri, null, null, null, null);
getContentResolver().query(uri, null, null, null, null);
我們可以看一下輸出
12-08 14:05:57.775 22494-22506/com.example.gyh.myapplication:provider I/Provider: query Binder_2
12-08 14:05:57.775 22494-22505/com.example.gyh.myapplication:provider I/Provider: query Binder_1
12-08 14:05:57.775 22494-22506/com.example.gyh.myapplication:provider I/Provider: query Binder_2
每次線程都不一樣,因?yàn)檫@些方法是運(yùn)作在Binder線程池里的 除了onCreate是運(yùn)行在主線程里现使,所以在onCreate是不能進(jìn)行耗時(shí)操作的低匙。
這樣簡(jiǎn)單的ContentProvider就使用成功了,但是為了更好是使用我們需要
結(jié)合SqliteOpenHelper去創(chuàng)建數(shù)據(jù)庫(kù)去存儲(chǔ)數(shù)據(jù),所以我們又建立了類去繼承SqliteOpenHelper去創(chuàng)建數(shù)據(jù)庫(kù)
public class DbOpenHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "book_provider.db";//數(shù)據(jù)庫(kù)名
public static final String BOOK_TABLE_NAME = "book";//數(shù)據(jù)表名
public static final String USER_TALBE_NAME = "user";
private static final int DB_VERSION = 3;//版本號(hào)
private String CREATE_BOOK_TABLE = "CREATE TABLE IF NOT EXISTS "
+ BOOK_TABLE_NAME + "(_id INTEGER PRIMARY KEY," + "name TEXT)";
private String CREATE_USER_TABLE = "CREATE TABLE IF NOT EXISTS "
+ USER_TALBE_NAME + "(_id INTEGER PRIMARY KEY," + "name TEXT,"
+ "sex INT)";
public DbOpenHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK_TABLE);//建表
db.execSQL(CREATE_USER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//用于版本更換的時(shí)候調(diào)用
}
然后我們?cè)趯?duì)ContentProvider進(jìn)行修改
public class BookProvider extends ContentProvider {
String TAG = "Provider";
static String AUTHORITY = "com.example.gyh.myapplication.ContentProvider_text.BookProvider";
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
public static final int BOOK_URI_CODE = 0;
public static final int USER_URI_CODE = 1;
static {
sUriMatcher.addURI(AUTHORITY, "book", BOOK_URI_CODE);
sUriMatcher.addURI(AUTHORITY, "user", USER_URI_CODE);
}
Context mContext;
SQLiteDatabase mDb;
@Override
//可以進(jìn)行一些初始化 該方法運(yùn)行在主線程里朴下,其他五個(gè)方法運(yùn)行在Binder線程池里
public boolean onCreate() {
mContext = getContext();
initProviderData();
return true;
}
private void initProviderData() {
mDb = new DbOpenHelper(mContext).getWritableDatabase();
mDb.execSQL("delete from " + DbOpenHelper.BOOK_TABLE_NAME);
mDb.execSQL("delete from " + DbOpenHelper.USER_TALBE_NAME);
mDb.execSQL("insert into book values(3,'Android');");
mDb.execSQL("insert into book values(4,'Ios');");
mDb.execSQL("insert into book values(5,'Html5');");
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Log.i(TAG, "query " + Thread.currentThread().getName());
String table = getTableName(uri);
if (table == null) {
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
return mDb.query(table, projection, selection, selectionArgs, null, null, sortOrder, null);
}
@Nullable
@Override
//用于返回Uri請(qǐng)求對(duì)于的MIME類型(媒體類型)
public String getType(Uri uri) {
Log.i(TAG, "getType " + Thread.currentThread().getName());
return null;
}
private String getTableName(Uri uri) {
String tableName = null;
switch (sUriMatcher.match(uri)) {
case BOOK_URI_CODE:
tableName = DbOpenHelper.BOOK_TABLE_NAME;
break;
case USER_URI_CODE:
tableName = DbOpenHelper.USER_TALBE_NAME;
break;
default:
break;
}
return tableName;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
Log.i(TAG, "insert " + Thread.currentThread().getName());
String tablename = getTableName(uri);
if (tablename == null) {
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
Log.i(TAG, "insert table name " + tablename);
mDb.insert(tablename, null, values);
mContext.getContentResolver().notifyChange(uri, null);
return uri;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
String table = getTableName(uri);
if (table == null) {
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
int count = mDb.delete(table, selection, selectionArgs);
if (count > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
Log.i(TAG, "update " + Thread.currentThread().getName());
String table = getTableName(uri);
if (table == null) {
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
int row = mDb.update(table, values, selection, selectionArgs);
if (row > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return row;
}
}
對(duì)Activity進(jìn)行修改
Uri bookUri = Uri.parse("content://com.example.gyh.myapplication.ContentProvider_text.BookProvider/book");
ContentValues values = new ContentValues();
values.put("_id", 6);
values.put("name", "程序設(shè)計(jì)的藝術(shù)");
getContentResolver().insert(bookUri, values);
Cursor bookCursor = getContentResolver().query(bookUri, new String[]{"_id", "name"}, null, null, null);
while (bookCursor.moveToNext()) {//這里的0指的是篩選的第一個(gè)條件就是_id
Log.d(TAG, "query book:" + bookCursor.getInt(0) + " " + bookCursor.getString(1));
}
bookCursor.close();
意思代碼完成了ContentProvider的基本使用
對(duì)上述代碼中可能大家會(huì)對(duì)Uri和UriMatcher的使用不是很了解 那么我來舉個(gè)例子
第一部初始化
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
第二部將Uri和UriMatcher配對(duì)
matcher.addURI("com.yfz.Lesson", "person/#", PEOPLE_ID); PEOPLE_ID是code int```
第三步我們就可以通過請(qǐng)求的Uri進(jìn)行操作
```Uri uri = Uri.parse("content://" + "com.yfz.Lesson" + "/people");
int match = matcher.match(uri);
switch (match)
{
case PEOPLE:
return "vnd.android.cursor.dir/people";
case PEOPLE_ID:
return "vnd.android.cursor.item/people";
default:
return null;
} ```
返回的結(jié)果就是"vnd.android.cursor.dir/person".
我們可以看到UriMatcher作用是可以組合Uri 這里將com.yfz.Lesson和people結(jié)合content://com.yfz.Lesson/people 所以我們后面輸入的Uri不再是content://com.yfz.Lesson而是content://com.yfz.Lesson/people努咐,我們可以通過輸入的Uri判斷出他們的Code,進(jìn)行一些判斷和操作殴胧。