可用广播,接口,provider等方式
创建数据库
public class DbHelper {
public static String P_SHARE_PREFENCE = "protocolData";
public static final String DOWNLOAD_TO_SDCARD_PATH = Environment
.getExternalStorageDirectory().getAbsolutePath()
+ "/data/kuaidi/";
public static final String SDCARD_PATH = Environment
.getExternalStorageDirectory() + "/";
public static final String BACKUP_TO_USERINFO = Environment
.getExternalStorageDirectory() + "/data/cache/backupinfo/";
public DbHelper(){
SQLiteDatabase db = getDb();
if (db == null){
return;
}
//db.execSQL("DROP TABLE IF EXISTS " + Provider.PersonColumns.TABLE_NAME);
db.execSQL("CREATE TABLE IF NOT EXISTS " + "test"
+ "( _id INTEGER PRIMARY KEY,"
+ "mCom VARCHAR,"
+ "mNu TEXT,"
+ "mName TEXT,"
+ "mAddTime TEXT,"
+ "mValicode TEXT,"
+ "mStatus TEXT,"
+ "mState TEXT,"
+ "mTag TEXT,"
+ "mMsg VARCHAR);");
db.execSQL("CREATE TABLE IF NOT EXISTS " + Provider.PersonColumns.TABLE_NAME + " (" + Provider.PersonColumns._ID
+ " INTEGER PRIMARY KEY," + Provider.PersonColumns.NAME + " TEXT," + Provider.PersonColumns.AGE
+ " INTEGER" + ");");
db.close();
}
private SQLiteDatabase getDb() {
boolean success = (new File(DOWNLOAD_TO_SDCARD_PATH + "db"))
.mkdirs();
if (success) {
}
try {
return SQLiteDatabase.openDatabase(DOWNLOAD_TO_SDCARD_PATH + "jacp_demo.db", null,
SQLiteDatabase.CREATE_IF_NECESSARY);
} catch (SQLException e) {
return null;
}
}
public void insert() {
SQLiteDatabase db = getDb();
ContentValues values = new ContentValues();
values.put(Provider.PersonColumns.NAME, "222");
values.put(Provider.PersonColumns.AGE, "2222");
db.insert("person", null, values);
}
}
/**
* 存放跟数据库有关的常量
*
* @author jacp
*
*/
public class Provider {
// 这个是每个Provider的标识,在Manifest中使用
public static final String AUTHORITY = "com.jacp.provider.demo.person";
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.jacp.demo";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.jacp.demo";
/**
* 跟Person表相关的常量
*
* @author jacp
*
*/
public static final class PersonColumns implements BaseColumns {
// CONTENT_URI跟数据库的表关联,最后根据CONTENT_URI来查询对应的表
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/persons");
public static final Uri CONTENT_URI1 = Uri.parse("content://" + AUTHORITY + "/persons1");
public static final String TABLE_NAME = "person";
public static final String DEFAULT_SORT_ORDER = "age desc";
public static final String NAME = "name";
public static final String AGE = "age";
}
}
public class Person {
public String name;
public int age;
}
通过ContentProvider来接受监听
public class PersonProvider extends ContentProvider { @Override public boolean onCreate() { return false; } @Nullable @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { return null; } @Nullable @Override public String getType(Uri uri) { return null; } @Nullable @Override public Uri insert(Uri uri, ContentValues values) { return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0; } }
使用
public class ContentProviderDemoActivity extends Activity implements OnClickListener {
public Button mButton1;
public Button mButton2;
public Button mButton3;
public Context mContext;
// public DatabaseHelper mDatabaseHelper;
// public SQLiteDatabase db;
public DbHelper mDbHelper;
private static final String TAG = "hrl";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
mDbHelper = new DbHelper();
// mDatabaseHelper = new DatabaseHelper(mContext);
// db = mDatabaseHelper.getWritableDatabase();
mButton1 = (Button) findViewById(R.id.insert);
mButton2 = (Button) findViewById(R.id.delete);
mButton3 = (Button) findViewById(R.id.update);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
getContentResolver().registerContentObserver(Provider.PersonColumns.CONTENT_URI, false,
new PersonObserver(new Handler()));
getContentResolver().registerContentObserver(Provider.PersonColumns.CONTENT_URI1, false,
new PersonObserver1(new Handler()));
}
private void insert() {
Person p = new Person();
p.name = "jacp";
p.age = 99;
int id = insert(p);
}
private int insert(Person person) {
ContentValues values = new ContentValues();
values.put(Provider.PersonColumns.NAME, person.name);
values.put(Provider.PersonColumns.AGE, person.age);
//db.insert("person", null, values);
Uri uri = getContentResolver().insert(Provider.PersonColumns.CONTENT_URI, values);
getContentResolver().notifyChange(Provider.PersonColumns.CONTENT_URI, null);
Log.i(TAG, "insert uri=" + uri);
String lastPath = uri.getLastPathSegment();
if (TextUtils.isEmpty(lastPath)) {
Log.i(TAG, "insert failure!");
} else {
Log.i(TAG, "insert success! the id is " + lastPath);
}
return Integer.parseInt(lastPath);
}
private void query(int id) {
Cursor c = getContentResolver().query(Provider.PersonColumns.CONTENT_URI,
new String[] { Provider.PersonColumns.NAME, Provider.PersonColumns.AGE },
Provider.PersonColumns._ID + "=?", new String[] { id + "" }, null);
if (c != null && c.moveToFirst()) {
Person p = new Person();
p.name = c.getString(c.getColumnIndexOrThrow(Provider.PersonColumns.NAME));
p.age = c.getInt(c.getColumnIndexOrThrow(Provider.PersonColumns.AGE));
Log.i(TAG, "person.name=" + p.name + "---person.age=" + p.age);
} else {
Log.i(TAG, "query failure!");
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.insert:
// insert();
break;
case R.id.delete:
// query(id);
Log.d(TAG, "insert !");
ContentValues values = new ContentValues();
values.put(Provider.PersonColumns.NAME, "222");
values.put(Provider.PersonColumns.AGE, "2222");
// db.insert("person", null, values);
// db.close();
Log.d(TAG, "insert !");
getContentResolver().notifyChange(Provider.PersonColumns.CONTENT_URI, null);
break;
case R.id.update:
new DbHelper().insert();
Log.d(TAG, "insert !");
getContentResolver().notifyChange(Provider.PersonColumns.CONTENT_URI1, null);
// getContentResolver().notifyChange(Provider.PersonColumns.CONTENT_URI, null);
break;
default:
break;
}
}
class PersonObserver extends ContentObserver {
public PersonObserver(Handler handler) {
super(handler);
}
public void onChange(boolean selfChange) {
// 此处可以进行相应的业务处理
Log.d("hrl", "onchanted");
}
}
class PersonObserver1 extends ContentObserver {
public PersonObserver1(Handler handler) {
super(handler);
}
public void onChange(boolean selfChange) {
// 此处可以进行相应的业务处理
Log.d("hrl", "onchaddddddddnted");
}
}
}
注册
其他相关文章
android使用sqliteopenhelper管理sd卡数据库
http://www.360doc.com/content/13/1211/18/9085503_336392860.shtml
http://stackoverflow.com/questions/14155757/howto-provide-notification-to-android-app-whenever-sqlite-table-updates
创建数据库
public class DbHelper {
public static String P_SHARE_PREFENCE = "protocolData";
public static final String DOWNLOAD_TO_SDCARD_PATH = Environment
.getExternalStorageDirectory().getAbsolutePath()
+ "/data/kuaidi/";
public static final String SDCARD_PATH = Environment
.getExternalStorageDirectory() + "/";
public static final String BACKUP_TO_USERINFO = Environment
.getExternalStorageDirectory() + "/data/cache/backupinfo/";
public DbHelper(){
SQLiteDatabase db = getDb();
if (db == null){
return;
}
//db.execSQL("DROP TABLE IF EXISTS " + Provider.PersonColumns.TABLE_NAME);
db.execSQL("CREATE TABLE IF NOT EXISTS " + "test"
+ "( _id INTEGER PRIMARY KEY,"
+ "mCom VARCHAR,"
+ "mNu TEXT,"
+ "mName TEXT,"
+ "mAddTime TEXT,"
+ "mValicode TEXT,"
+ "mStatus TEXT,"
+ "mState TEXT,"
+ "mTag TEXT,"
+ "mMsg VARCHAR);");
db.execSQL("CREATE TABLE IF NOT EXISTS " + Provider.PersonColumns.TABLE_NAME + " (" + Provider.PersonColumns._ID
+ " INTEGER PRIMARY KEY," + Provider.PersonColumns.NAME + " TEXT," + Provider.PersonColumns.AGE
+ " INTEGER" + ");");
db.close();
}
private SQLiteDatabase getDb() {
boolean success = (new File(DOWNLOAD_TO_SDCARD_PATH + "db"))
.mkdirs();
if (success) {
}
try {
return SQLiteDatabase.openDatabase(DOWNLOAD_TO_SDCARD_PATH + "jacp_demo.db", null,
SQLiteDatabase.CREATE_IF_NECESSARY);
} catch (SQLException e) {
return null;
}
}
public void insert() {
SQLiteDatabase db = getDb();
ContentValues values = new ContentValues();
values.put(Provider.PersonColumns.NAME, "222");
values.put(Provider.PersonColumns.AGE, "2222");
db.insert("person", null, values);
}
}
/**
* 存放跟数据库有关的常量
*
* @author jacp
*
*/
public class Provider {
// 这个是每个Provider的标识,在Manifest中使用
public static final String AUTHORITY = "com.jacp.provider.demo.person";
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.jacp.demo";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.jacp.demo";
/**
* 跟Person表相关的常量
*
* @author jacp
*
*/
public static final class PersonColumns implements BaseColumns {
// CONTENT_URI跟数据库的表关联,最后根据CONTENT_URI来查询对应的表
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/persons");
public static final Uri CONTENT_URI1 = Uri.parse("content://" + AUTHORITY + "/persons1");
public static final String TABLE_NAME = "person";
public static final String DEFAULT_SORT_ORDER = "age desc";
public static final String NAME = "name";
public static final String AGE = "age";
}
}
public class Person {
public String name;
public int age;
}
通过ContentProvider来接受监听
public class PersonProvider extends ContentProvider { @Override public boolean onCreate() { return false; } @Nullable @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { return null; } @Nullable @Override public String getType(Uri uri) { return null; } @Nullable @Override public Uri insert(Uri uri, ContentValues values) { return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0; } }
使用
public class ContentProviderDemoActivity extends Activity implements OnClickListener {
public Button mButton1;
public Button mButton2;
public Button mButton3;
public Context mContext;
// public DatabaseHelper mDatabaseHelper;
// public SQLiteDatabase db;
public DbHelper mDbHelper;
private static final String TAG = "hrl";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
mDbHelper = new DbHelper();
// mDatabaseHelper = new DatabaseHelper(mContext);
// db = mDatabaseHelper.getWritableDatabase();
mButton1 = (Button) findViewById(R.id.insert);
mButton2 = (Button) findViewById(R.id.delete);
mButton3 = (Button) findViewById(R.id.update);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
getContentResolver().registerContentObserver(Provider.PersonColumns.CONTENT_URI, false,
new PersonObserver(new Handler()));
getContentResolver().registerContentObserver(Provider.PersonColumns.CONTENT_URI1, false,
new PersonObserver1(new Handler()));
}
private void insert() {
Person p = new Person();
p.name = "jacp";
p.age = 99;
int id = insert(p);
}
private int insert(Person person) {
ContentValues values = new ContentValues();
values.put(Provider.PersonColumns.NAME, person.name);
values.put(Provider.PersonColumns.AGE, person.age);
//db.insert("person", null, values);
Uri uri = getContentResolver().insert(Provider.PersonColumns.CONTENT_URI, values);
getContentResolver().notifyChange(Provider.PersonColumns.CONTENT_URI, null);
Log.i(TAG, "insert uri=" + uri);
String lastPath = uri.getLastPathSegment();
if (TextUtils.isEmpty(lastPath)) {
Log.i(TAG, "insert failure!");
} else {
Log.i(TAG, "insert success! the id is " + lastPath);
}
return Integer.parseInt(lastPath);
}
private void query(int id) {
Cursor c = getContentResolver().query(Provider.PersonColumns.CONTENT_URI,
new String[] { Provider.PersonColumns.NAME, Provider.PersonColumns.AGE },
Provider.PersonColumns._ID + "=?", new String[] { id + "" }, null);
if (c != null && c.moveToFirst()) {
Person p = new Person();
p.name = c.getString(c.getColumnIndexOrThrow(Provider.PersonColumns.NAME));
p.age = c.getInt(c.getColumnIndexOrThrow(Provider.PersonColumns.AGE));
Log.i(TAG, "person.name=" + p.name + "---person.age=" + p.age);
} else {
Log.i(TAG, "query failure!");
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.insert:
// insert();
break;
case R.id.delete:
// query(id);
Log.d(TAG, "insert !");
ContentValues values = new ContentValues();
values.put(Provider.PersonColumns.NAME, "222");
values.put(Provider.PersonColumns.AGE, "2222");
// db.insert("person", null, values);
// db.close();
Log.d(TAG, "insert !");
getContentResolver().notifyChange(Provider.PersonColumns.CONTENT_URI, null);
break;
case R.id.update:
new DbHelper().insert();
Log.d(TAG, "insert !");
getContentResolver().notifyChange(Provider.PersonColumns.CONTENT_URI1, null);
// getContentResolver().notifyChange(Provider.PersonColumns.CONTENT_URI, null);
break;
default:
break;
}
}
class PersonObserver extends ContentObserver {
public PersonObserver(Handler handler) {
super(handler);
}
public void onChange(boolean selfChange) {
// 此处可以进行相应的业务处理
Log.d("hrl", "onchanted");
}
}
class PersonObserver1 extends ContentObserver {
public PersonObserver1(Handler handler) {
super(handler);
}
public void onChange(boolean selfChange) {
// 此处可以进行相应的业务处理
Log.d("hrl", "onchaddddddddnted");
}
}
}
注册
其他相关文章
android使用sqliteopenhelper管理sd卡数据库
http://www.360doc.com/content/13/1211/18/9085503_336392860.shtml
http://stackoverflow.com/questions/14155757/howto-provide-notification-to-android-app-whenever-sqlite-table-updates