Home > Java > javaTutorial > body text

Introduction to monitoring data changes in ContentProvider based on Android

高洛峰
Release: 2017-02-07 15:53:52
Original
1266 people have browsed it

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方法上}
Copy after login

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);
  }  }
}
Copy after login

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);
   }
  });
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!