Android

来源:互联网 发布:工作流设计软件 编辑:程序博客网 时间:2024/05/23 14:37

功能内容介绍:三个应用,有一个包含ContentProvider的应用(MainApp),该应用在PersonProvider类中的insert方法中包含

getContext().getContentResolver().notifyChange(insertUri, null);

即可监听,A应用通过ContentResolver类往MainApp中添加一条记录,B应用可以通过ContentResolver的query方法查找到插入的那条数据

MainApp应用的PersonProvider.java

package com.example.test_sqlite;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;import com.example.test_sqlite.service.SQLHelper;public class PersonProvider extends ContentProvider{private SQLHelper sqlHelper = null;private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);// NO_MATCH值为-1private static final int PERSONS = 1;private static final int PERSON = 2;private final String TABLE_NAME_PERSON = "person";static{MATCHER.addURI("com.lipeng.providers.personprovider", "person", PERSONS);MATCHER.addURI("com.lipeng.providers.personprovider", "person/#", PERSON);}@Overridepublic boolean onCreate() {sqlHelper = new SQLHelper(getContext());return true;}@Overridepublic Uri insert(Uri uri, ContentValues values) {SQLiteDatabase db = sqlHelper.getWritableDatabase();switch (MATCHER.match(uri)) {case PERSONS:long rowid = db.insert(TABLE_NAME_PERSON, "name", values); // 主键值Uri insertUri = ContentUris.withAppendedId(uri, rowid);getContext().getContentResolver().notifyChange(insertUri, null);// 发出数据变化通知return insertUri;default:throw new IllegalArgumentException("this is Unknow Uri:" + uri);}}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {SQLiteDatabase db = sqlHelper.getWritableDatabase();int num = 0;switch (MATCHER.match(uri)) {case PERSONS:num = db.delete(TABLE_NAME_PERSON, selection, selectionArgs);break;case PERSON:long rowid = ContentUris.parseId(uri);String where = "personid=" + rowid;if(selection != null && !"".equals(selection.trim())){where += " and " + selection;}num = db.delete(TABLE_NAME_PERSON, where, selectionArgs);break;default:throw new IllegalArgumentException("this is Unknow Uri:" + uri);}return num;}@Overridepublic int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {SQLiteDatabase db = sqlHelper.getWritableDatabase();switch (MATCHER.match(uri)) {case PERSONS:return db.update(TABLE_NAME_PERSON, values, selection, selectionArgs);case PERSON:long rowid = ContentUris.parseId(uri);String where = "personid=" + rowid;if(selection != null && !"".equals(selection.trim())){where += " and " + selection;}return db.update(TABLE_NAME_PERSON, values, where, selectionArgs);default:throw new IllegalArgumentException("this is Unknow Uri:" + uri);}}/** * projection 要查询的字段 */@Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {// 约定:/person表示查询表的所有数据;/person/10表示查询id为10的数据SQLiteDatabase db = sqlHelper.getReadableDatabase();switch (MATCHER.match(uri)) {case PERSONS:return db.query(TABLE_NAME_PERSON, projection, selection, selectionArgs, null, null, sortOrder);case PERSON:long rowid = ContentUris.parseId(uri);String where = "personid=" + rowid;if(selection != null && !"".equals(selection.trim())){where += " and " + selection;}return db.query(TABLE_NAME_PERSON, null, where, selectionArgs, null, null, sortOrder);default:throw new IllegalArgumentException("this is Unknow Uri:" + uri);}}@Overridepublic String getType(Uri uri) {switch (MATCHER.match(uri)) {case PERSONS:return "vnd.android.cursor.dir/person";// 多条记录case PERSON:return "vnd.android.cursor.item/person"; // 单条记录default:throw new IllegalArgumentException("this is Unknow Uri:" + uri);}}}

A应用:MainActivity.java

package com.example.aapp;import android.net.Uri;import android.os.Bundle;import android.app.Activity;import android.content.ContentResolver;import android.content.ContentValues;import android.view.Menu;import android.view.View;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic 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;}/** * 模板是public void onclick(View view){},返回值必须为void,参数为View * */public void insert(View view){String uriStr = "content://com.lipeng.providers.personprovider/person";Uri uri = Uri.parse(uriStr);ContentResolver resolver = getContentResolver();ContentValues values = new ContentValues();values.put("name", "Aapp");values.put("phone", "132323232323232");values.put("amount", "800000000");resolver.insert(uri, values);}}

B应用:MainActivity.java
package com.example.test;import android.app.Activity;import android.database.ContentObserver;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.Menu;public class MainActivity extends Activity {private final static String TAG = "MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Uri uri = Uri.parse("content://com.lipeng.providers.personprovider/person");this.getContentResolver().registerContentObserver(uri, true,new PersonContentObserver(new Handler()));}@Overridepublic 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;}private class PersonContentObserver extends ContentObserver {public PersonContentObserver(Handler handler) {super(handler);}@Overridepublic void onChange(boolean selfChange, Uri uri) {Cursor cursor = getContentResolver().query(uri, null, null, null,"personid asc limit 1");if (cursor.moveToFirst()) {String name = cursor.getString(cursor.getColumnIndex("name"));String phone = cursor.getString(cursor.getColumnIndex("phone"));int amount = cursor.getInt(cursor.getColumnIndex("amount"));Log.i(TAG, "name=" + name + ", phone=" + phone + ", amount=" + amount);}}}}

0 0
原创粉丝点击