步骤
权限在application中注册
- <provider
- android:name=".BatchPrivoder"
- android:authorities="cn.example.providers.batchProvider"
- android:exported="true"
- >
- </provider>
在app的包名下或者子包中自定义contentProvider
- package com.example.nfcassistant;
- import com.example.engine.DBOpenHelper;
- import android.content.ContentProvider;
- import android.content.ContentUris;
- import android.content.ContentValues;
- import android.content.UriMatcher;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.net.Uri;
- /**
- * contentProvider 作为系统的组件应该放在应用的包下com.example.nfcassistant;,或者子包下 必须在配置文件中注册
- * <provider android:name=".BatchPrivoder"
- * android:authorities="cn.example.providers.batch" > </provider>
- *
- * @author ming
- *
- */
- public class BatchPrivoder extends ContentProvider {
- DBOpenHelper dbOpenHelper;
- // 实例化一个工具类用来验证出入的路径是否匹配,UriMatcher.NO_MATCH默认的不匹配时的状态码,其实就是-1
- private static final UriMatcher MATCHER = new UriMatcher(
- UriMatcher.NO_MATCH);
- private static final int BATCHS = 1;// 定义一个状态码,判断路径是否是对应batch
- private static final int BATCH = 2;// 定义一个状态码,判断路径是否是对应
- // 操作静态类应该用静态代码块
- static {
- MATCHER.addURI("cn.example.providers.batchProvider", "batch", BATCHS);// 路径是batch这种的一般是匹配全部,BATCHS匹配的时候返回的状态吗
- MATCHER.addURI("cn.example.providers.batchProvider", "batch/#", BATCH);// 路径是batch这种的一般是匹配某一个,BATCH匹配的时候返回的状态吗
- }
- @Override
- public int delete(Uri uri, String selection, String[] selectionArgs) {
- int num = 0;
- SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
- switch (MATCHER.match(uri)) {
- case 1:// 删除全部
- num = db.delete("tbl_batch", selection, selectionArgs);
- break;
- case 2:// 删除对应的
- long rowid = ContentUris.parseId(uri);// 通过这个方法得到uri路径最后要具体操作的id
- String where = "DeviceId=" + rowid;// 拼接where语句
- System.out.println(where);
- if (selection != null && !"".equals(selection.trim())) {
- where = where + " and " + selection;// 当传进来的where条件不为空是,和当前的条件拼接
- }
- num = db.delete("tbl_batch", where, selectionArgs);
- break;
- default:
- throw new IllegalArgumentException("这是一个未知的路径" + uri);// 当不匹配的时候抛出非法参数例外
- }
- return num;
- }
- /**
- * 设置内容的格式
- *
- * @param uri
- * @return
- */
- @Override
- public String getType(Uri uri) {
- // TODO Auto-generated method stub
- return null;
- }
- /**
- * uri:传入的应该判断是否匹配 values 要插入的值
- */
- @Override
- public Uri insert(Uri uri, ContentValues values) {
- SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
- switch (MATCHER.match(uri)) {// 验证传入的uri通过实例化的工具类
- case 1:// 符合:content://cn.example.providers.batchProvider/batch
- // 这种路径的因为是要向batch表中插入数据
- long rowid = db.insert("tbl_batch", "DeviceId", values);// DeviceId当values出入NULL的时候,就向DeviceId字段传入null
- // rowid 当表有自增长的主键ID时,返回的rowid就是主键
- // contentProvider的insert要求返回一个Uri,这个Uri应该代表插入的这这条数据所以应该组拼一条Uri
- // content://cn.example.providers.batch/batch/rowid
- Uri insertUri = Uri
- .parse("content://cn.example.providers.batchProvider/batch"
- + rowid);
- // 或者使用一个工具类直接组装
- // Uri insertUri = ContentUris.withAppendedId(uri, rowid);
- // 推荐使用这种,和上边的效果是一样的
- System.out
- .println("content://cn.example.providers.batchProvider/batch"
- + rowid);
- return insertUri;
- default:
- throw new IllegalArgumentException("这是一个位置的路径" + uri);// 当不匹配的时候抛出非法参数例外
- }
- }
- /**
- * 初始化工作 比如得到数据库操作对象
- *
- * @return
- */
- @Override
- public boolean onCreate() {
- dbOpenHelper = new DBOpenHelper(this.getContext());
- return true;
- }
- @Override
- public Cursor query(Uri uri, String[] projection, String selection,
- String[] selectionArgs, String sortOrder) {
- Cursor num;
- SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
- switch (MATCHER.match(uri)) {
- case 1:// 查询全部
- num = db.query("tbl_batch", projection, selection, selectionArgs,
- null, null, sortOrder);
- break;
- case 2:// 查询对应的
- long rowid = ContentUris.parseId(uri);// 通过这个方法得到uri路径最后要具体操作的id
- String where = "DeviceId=" + rowid;// 拼接where语句
- System.out.println(where);
- if (selection != null && !"".equals(selection.trim())) {
- where = where + " and " + selection;// 当传进来的where条件不为空是,和当前的条件拼接
- }
- num = db.query("tbl_batch",projection , where, selectionArgs, null, null, sortOrder);
- break;
- default:
- throw new IllegalArgumentException("这是一个未知的路径" + uri);// 当不匹配的时候抛出非法参数例外
- }
- return num;
- }
- @Override
- public int update(Uri uri, ContentValues values, String selection,
- String[] selectionArgs) {
- int num = 0;
- SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
- switch (MATCHER.match(uri)) {
- case 1:// 删除全部
- num = db.update("tbl_batch", values, selection, selectionArgs);
- break;
- case 2:// 删除对应的
- long rowid = ContentUris.parseId(uri);// 通过这个方法得到uri路径最后要具体操作的id
- String where = "DeviceId=" + rowid;// 拼接where语句
- System.out.println(where);
- if (selection != null && !"".equals(selection.trim())) {
- where = where + " and " + selection;// 当传进来的where条件不为空是,和当前的条件拼接
- }
- num = db.update("tbl_batch", values, selection, selectionArgs);
- break;
- default:
- throw new IllegalArgumentException("这是一个未知的路径" + uri);// 当不匹配的时候抛出非法参数例外
- }
- return num;
- }
- }
在其他app中测试
- package com.example.testcontentprovider;
- import android.app.Activity;
- import android.content.ContentResolver;
- import android.content.ContentValues;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- Button button;
- Button del;
- Button query;
- Button updata;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button = (Button) findViewById(R.id.button1);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Uri uri = Uri
- .parse("content://cn.example.providers.batchProvider/batch");
- // 要的到contentProvider应该通过下面的这个类
- ContentResolver resolver = getContentResolver();// 通过上下文
- ContentValues values = new ContentValues();
- values.put("DeviceId", "121");
- values.put("Codes", "ss");
- values.put("DeviceName", "牛皮纸");
- values.put("Price", "15");
- values.put("Custodian", "妮妮");
- values.put("End_user", "asd");
- values.put("Time", "19956565");
- resolver.insert(uri, values);
- }
- });
- del = (Button) findViewById(R.id.button2);
- del.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Uri uri = Uri
- .parse("content://cn.example.providers.batchProvider/batch/121");
- // 要的到contentProvider应该通过下面的这个类
- ContentResolver resolver = getContentResolver();// 通过上下文
- System.out.println("删除");
- resolver.delete(uri, null, null);
- }
- });
- query = (Button) findViewById(R.id.button3);
- query.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Uri uri = Uri
- .parse("content://cn.example.providers.batchProvider/batch/333");
- // 要的到contentProvider应该通过下面的这个类
- ContentResolver resolver = getContentResolver();// 通过上下文
- Cursor cursor = resolver
- .query(uri, null, null, null, "_id asc");
- cursor.moveToFirst();
- String id = cursor.getString(cursor.getColumnIndex("DeviceId"));
- System.out.println("id等于" + id);
- String tag = "id等于";
- Log.i(tag, id);
- }
- });
- updata = (Button) findViewById(R.id.button4);
- updata.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Uri uri = Uri
- .parse("content://cn.example.providers.batchProvider/batch/333");
- // 要的到contentProvider应该通过下面的这个类
- ContentResolver resolver = getContentResolver();// 通过上下文
- ContentValues values = new ContentValues();
- values.put("Time", "121");
- resolver.update(uri, values, null, null);
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
设置type
监听内容提供者数据变化
1,A应用更改数据
2,内容提供者发送内容更改通知
this.getContext().getContentResolver().notifyChange(uri,null);
哪条数据被更改,null,就是给所有发通知
3.得到通知的应用,做出相应
select * from person orer by persionid desc limit 1
得到最后加入的数据