一般地,正确的自动更新UI界面的方式是通过自动更新Cursor,而实现自动更新Cursor,则是在创建Cursor和和被请求处理之前先调用 Cursor#setNotificationUri,然后在更新Cursor的Uri namespace变化的时候调用ContentResolver#notifyChange。这样就实现了自动更新UI界面,而这些全部都可以在ContentProvider完成。

For example, suppose you were creating a simple mail application and you wanted to update when new mail arrived but also provide various views on the mail. I'd have some basic Uri's defined.

content://org.example/all_mail

content://org.example/labels

content://org.example/messages

Now, say I wanted to get a cursor that gave me all mail and be updated when new mail arrives:

Cursor c;

//code to get data

c.setNotificationUri(getContentResolver(), Uri.parse("content://org.example/all_mail");

Now new mail arrives so I notify:

//Do stuff to store in database

getContentResolver().notifyChange(Uri.parse("content://org.example/all_mail", null);

I should also notify all the Cursors that selected for labels this new message met

for(String label : message.getLabels() {

getContentResolver().notifyChange(Uri.parse("content://org.example/lables/" + label, null);

}

And also, maybe a cursor is viewing that one specific message so notify them as well:

getContentResolver().notifyChange(Uri.parse("content://org.example/messages/" + message.getMessageId(), null);

The getContentResolver() calls happen where the data is accessed. So if it's in a Service or ContentProvider that is where you setNotificationUri and notifyChange. You should not be doing that from where the data is accessed, e.g., an Activity.

AlarmProvider.java

更多相关文章

  1. android M拨号盘开源之旅(二)--- 浅析拨号盘主界面
  2. Android中的Menu的简介(翻译自官方文档)——————个人笔记
  3. Android应用集成开发环境,强烈推荐使用
  4. Android(安卓)Volley 网络请求框架图
  5. Android(安卓)App调用MediaRecorder实现录音功能的实例
  6. DroidBox简介
  7. Android学习轨迹之一:Android(安卓)BroadcastReceiver 学习
  8. Android(安卓)Camera数据流分析全程记录(overlay方式)
  9. android jni方法模拟高频按键点击

随机推荐

  1. MVP -----个人理解与示例(android例子 实
  2. android中TextView和EditText控件一些属
  3. Android显示图片自适应(控件+网页)
  4. Delphi xe7 up1 调用android振动功能
  5. Android 开源组件和第三方库汇总
  6. Android studio 出现 Unsupported major.
  7. Android 弹出键盘向上顶布局
  8. android 如何判断去电或来电已经接通
  9. Android Binder的使用和设计[android nat
  10. AndroidStudio试用