ContentProvider 监听数据变化

来源:互联网 发布:海关数据免费查询 编辑:程序博客网 时间:2024/05/01 13:03

一.流程图


二.C应用的主要方法,基于上一篇文章




/**

*允许外部应用插入数据

*/

@Override

publicUri insert(Uri uri, ContentValues values) {

SQLiteDatabasedbOperate =dbOpenHelper.getWritableDatabase();

//uri进行判断,看是不是合法的uri

switch(MATCHER.match(uri)){

case1:

//如果主键是整型的并且是自增长的,那么行号就等于主键

longrowId = dbOperate.insert("person","name",values);

Uri insertUri =Uri.parse("content://com.darren.provider/person/"+ rowId);

//Uri中追加参数

//ContentUris.withAppendedId(contentUri, id)

//同样的效果

//UriinsertUri = ContentUris.withAppendedId(uri,rowId);

//这一部分代码有所变化

//第二个参数表示数据变化的监听者null表示不设一定得到通知的监听者

//其他监听者监听的话还可以得到通知的

this.getContext().getContentResolver().notifyChange(uri,null);

returninsertUri;


default:

//如果参数错误,不存在这样的Uri就会抛出错误

thrownewIllegalArgumentException("this isUnknown Uri: " + uri);

}

}


三.A引用插入数据

packagecom.example.aapp;


importandroid.net.Uri;

importandroid.os.Bundle;

importandroid.app.Activity;

importandroid.content.ContentResolver;

importandroid.content.ContentValues;

importandroid.view.Menu;

importandroid.view.View;


publicclassMainActivityextendsActivity {


@Override

protectedvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}


@Override

publicbooleanonCreateOptionsMenu(Menu menu) {

//Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main,menu);

returntrue;

}


publicvoidinsert(View v) {

Uri uri =Uri.parse("content://com.darren.provider/person");

ContentValuesvalues =newContentValues();

values.put("name","A-App");

values.put("phone","123456781");

values.put("amount","5400");

ContentResolvercontentResolver =this.getContentResolver();

//这个方法内部会调用内容提供者的insert方法

contentResolver.insert(uri,values);

}

}


四.B应用监听并收到数据

packagecom.example.bapp;


importandroid.net.Uri;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.app.Activity;

importandroid.database.ContentObserver;

importandroid.database.Cursor;

importandroid.util.Log;

importandroid.view.Menu;


publicclassMainActivityextendsActivity {


@Override

protectedvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


Uri uri =Uri.parse("content://com.darren.provider/person");

Handler handler =newHandler();

PersonContentObsererobserver =newPersonContentObserer(handler);

//Uri中的数据发生变化的时候就会调用observer中的onChange方法

//注册数据通知

this.getContentResolver().registerContentObserver(uri,true,observer);

}


@Override

publicbooleanonCreateOptionsMenu(Menu menu) {

//Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main,menu);

returntrue;

}


privateclassPersonContentObsererextendsContentObserver {


publicPersonContentObserer(Handler handler) {

super(handler);

//TODOAuto-generated constructor stub

}


@Override

publicvoidonChange(booleanselfChange) {

Uri uri =Uri.parse("content://com.darren.provider/person");

Cursor cursor =getContentResolver().query(uri,null,null,null,"personid desc limit 1");

if(cursor.moveToFirst()) {

String name= cursor.getString(cursor.getColumnIndex("name"));

Log.i("xxx","xxx: "+ name);

}

}


}

}



五.监听结果

可以才控制台看到如下信息

xxx: A-App


原创粉丝点击