1內容訪問者
*內容訪問者:android
1.獲取提供者:getcontentResolverO;ContentResolver對象
2.訪問內容提供者:增洲改查ide
ContentResolver resolver=getcontentResolverO;
uri uri=uri.parse("content://com.provider111/query");
cursor cursor=resolver.query(uri,nu11,nu11,nu11,nu11);
if(cursor!=nu11&&cursor.getcount()>0){
while(cursor.moveToNextO){
long_id=cursor.getInt(0);
string sname=cursor.getstring(1);
string sno=cursor.getstring(2);
string sex=cursor.getstring(3):
int img=cursor.getInt(4);
student student=new Student(_id,sname,sno,sex,img);
system.out.println(student);server
2內容觀察者使用對象
Uri uri2=Uri. parse("content: // com.provider/delete");生命週期
/註冊層內容觀察者事件
resolver. registerContentobserver(uri2, true, new Contentobserver(new HandlerO)get
/監聽內容提供者數據發生改變string
public void onChange(boolean selfchange)it
super. onchange(selfchange);io
System.out. printin數據發生改變}
}
}
};
2在內容提供者添加通知內容觀察者代碼
insert. update delete方法中
Uri uri2=Uri. parse("content: /com, provider/insert")
getcontext ) getcontentResolver. notifychange(uri2,nu11);
3什麼是廣播接收者
Android系統中,內置了不少系統級別的廣播,例如手機開機完成後會發送一條廣播,電池電量不足時會發送一條廣播等。
AndroidBroadcastRe系統中提供了廣播接收者,廣播接收者的做用就是接收來自系統或其餘應用程序的廣播,並做出迴應。
4廣播接收者建立與註冊
要使用廣播接收者接收其餘應用程序發出的廣播,先要在本應用中建立廣播接收者並進行註冊。
5廣播接收者建立與註冊
註冊廣播有兩種方式,常駐型廣播與很是駐型廣播。
一、註冊常駐型廣播
常駐型廣播是當應用程序關閉後,若是接收到其餘應用程序發出的廣播那麼該程序會自動從新啓動。常駐型廣播在清單文件中註冊。
<action android: name=「android. orovider. TelephonySMS RECEIVED」
註冊廣播有兩種方式,常駐型廣播與很是駐型廣播。
二、註冊很是駐型廣播
很是駐型廣播依賴於註冊廣播的組件的生命週期,例如,在 Activity中註冊播接收者,當 Activity銷燬後廣播也隨之被移除這種廣播事件在代碼中註冊
MyBroadCastReceiver receiver new MyBroadCastReceiver(;
String action =「android. provider. Telephony.SMS_RECEIVED」
IntentFilter intentFilter new IntentFilter(action):
registerReceiver(receiver, intentFilter):
很是駐型廣播能夠在 onDestory方法中解除廣播具體代碼以下
unregisterReceiver(receiver);