自定义CursorLoader基本原理简单介绍

 

1、在配置清单添加需要的权限

 

 <uses-permission android:name="android.permission.READ_SMS"/>

 

 

2、res/layout下2个布局activity_main.xml布局和item_activity.xml布局

 

activity_main.xml布局

 

代码

 

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />

</RelativeLayout>

 

 

================

 

 

item_activity.xml布局

 

代码

 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />
    <TextView
        android:id="@+id/text_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />

</LinearLayout>

 

 

 

==============================

 

 

3、MainActivity.java类

 

代码

 

 

/**
 *
 * @author  Administrator
 * 自定义CursorLoader要实现LoaderCallbacks类观察者的原理
 *
 */
public class MainActivity extends Activity {

 private String uri_sms = "content://sms";
 private ListView listView;
 private SimpleCursorAdapter adapter;
 private Cursor cursor;

 private boolean issameObject;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  this.listView = (ListView) this.findViewById(R.id.listview);

  adapter = new SimpleCursorAdapter(this, R.layout.item_activity, cursor,
    new String[] { "body", "address" }, new int[] {
      R.id.text_phone, R.id.text_content },
    CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
  listView.setAdapter(adapter);
         
  new MyAsynTask().execute();
 }

 class MyAsynTask extends AsyncTask<Void, Void, Cursor> {

  @Override
  protected void onPreExecute() {
   super.onPreExecute();
  }

  @Override
  protected Cursor doInBackground(Void... params) {
   ContentResolver resolver = getContentResolver();
   // 查询数据库
   cursor = resolver.query(Uri.parse(uri_sms), new String[] { "_id",
     "body", "address" }, null, null, "date desc");
   return cursor;
  }

  @Override
  protected void onPostExecute(Cursor result) {
   super.onPostExecute(result);
   // 注册一个观察者
   result.registerContentObserver(new Observer(null));
   //交换数据
   adapter.swapCursor(result);
   issameObject = false;
  }

  // 观察者类 要继承ContentObserver类
  class Observer extends ContentObserver {

   public Observer(Handler handler) {
    super(handler);
   }
   @Override//注册对象发生改变 都要触发该方法
   public void onChange(boolean selfChange) {
    if (issameObject) {//如果注册的对象 是同一个 什么都不做
     return;
    }
    new MyAsynTask().execute();//如果注册对象改变了 就重新启动MyAsynTask类
    issameObject = true;//改变标志
   }

  }

 }
}

 

 

 

转载于:https://my.oschina.net/u/2542711/blog/615263

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值