1629. 按鍵持續(xù)時(shí)間最長(zhǎng)的鍵
640
2025-04-01
@TOC
一、內(nèi)容提供者概述
二、創(chuàng)建內(nèi)容提供者
三、訪問(wèn)其他應(yīng)用程序
1、查詢其他程序的數(shù)據(jù)
//獲取相應(yīng)操作的Uri,Uri.parse()方法是將字符串轉(zhuǎn)化成Uri對(duì)象。 Uri uri = Uri.parse("content://cn.itcast.mycontentprovider/person"); //獲取ContentResolver對(duì)象 ContentResolver resolver = context.getContentResolver(); //通過(guò)ContentResolver對(duì)象查詢數(shù)據(jù) Cursor cursor = resolver.query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder); while (cursor.moveToNext()) { String address = cursor.getString(0); long date = cursor.getLong(1); int type = cursor.getInt(2); } cursor.close();
2 、查詢系統(tǒng)短信
四、內(nèi)容觀察者
1、什么是內(nèi)容觀察者
//創(chuàng)建內(nèi)容觀察者 private class MyObserver extends ContentObserver{ public MyObserver(Handler handler) { super(handler); } //當(dāng)觀察到Uri代表的數(shù)據(jù)發(fā)生變化時(shí)調(diào)用此方法,程序會(huì)回調(diào)onChange()方法, //并在該方法中處理相關(guān)邏輯 public void onChange(boolean selfChange) { super.onChange(selfChange); } }
ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://aaa.bbb.ccc"); //注冊(cè)內(nèi)容觀察者 resolver.registerContentObserver(uri, true, new MyObserver(new Handler()));
@Override protected void onDestroy() { super.onDestroy(); //取消注冊(cè)內(nèi)容觀察者 getContentResolver().unregisterContentObserver(new MyObserver( new Handler())); }
2、監(jiān)測(cè)數(shù)據(jù)變化
五、本章小結(jié)
本章詳細(xì)地講解了內(nèi)容提供者的相關(guān)知識(shí),首先簡(jiǎn)單地介紹了內(nèi)容提供者,然后講解了如何創(chuàng)建內(nèi)容提供者以及如何使用內(nèi)容提供者訪問(wèn)其他程序暴露的數(shù)據(jù),最后講解內(nèi)容觀察者,通過(guò)內(nèi)容觀察者觀察數(shù)據(jù)的變化。本章所講的ContentProvider是Android四大組件之一,在后續(xù)遇到程序之間需要共享數(shù)據(jù)時(shí),會(huì)經(jīng)常用到該組件,因此要求初學(xué)者一定要熟練掌握本章內(nèi)容。
為了記住你的笑容,我拼命按下心中的快門!——《美麗人生》
Android 移動(dòng)應(yīng)用測(cè)試 MobileAPPTest
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。