Home  >  Article  >  Java  >  Introduction to monitoring data changes in ContentProvider based on Android

Introduction to monitoring data changes in ContentProvider based on Android

高洛峰
高洛峰Original
2017-02-07 15:53:521254browse

If visitors to ContentProvider need to know the changes in the data in ContentProvider, they can call getContentResolver().notifyChange(uri,null) when the data in ContentProvider changes to notify visitors registered on this URI.

public class PersonContentProvider extends ContentProvider[
 public Uri insert(Uri uri,ContentValues values){
  db.insert("person","personid",values);
  getContext().getContentResolver().notifyChange(uri,null);
 }//通知注册在此URI上的访问者,此外注册在insert方法上}

If visitors to ContentProvider need to be notified of data changes, they must use ContentObserver to monitor the data (the data is described by URI). When the data change notification is monitored, the system will call the onChange() method of ContentObserver. .

public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Uri uri = Uri.parse("content://cn.wordtech.providers.personprovider/person");
  this.getContentResolver().registerContentObserver(uri, true, new PersonContentdObserver(new Handler()));
  // 第三个对象为监听对象,当数据发生改变的时候通知此对象做相应的改变
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }
 private class PersonContentdObserver extends ContentObserver {
  public PersonContentdObserver(Handler handler) {
   super(handler);
  }
  @Override
  public void onChange(boolean selfChange) {
   Uri uri = Uri.parse("content://cn.wordtech.providers.personprovider/person");
   Cursor cursor = getContentResolver().query(uri, null, null, null,"personid desc limit 1");
   while (cursor.moveToNext()) {
    String name = cursor.getString(cursor.getColumnIndex("name"));
    Log.i("Name", name);
   }
   super.onChange(selfChange);
  }  }
}

Test application:

Button btn = (Button) findViewById(R.id.btn);
  btn.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    Uri uri = Uri.parse("content://cn.wordtech.providers.personprovider/person");// 根据标识名得到内容提供者
    ContentResolver cr = MainActivity.this.getContentResolver();
    ContentValues values = new ContentValues();
    values.put("name", "Livingstone");
    values.put("phone", "1101");
    values.put("amount", "1111111111");
    cr.insert(uri, values);
   }
  });

For more related articles on monitoring data changes in ContentProvider based on Android, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn