大V的回帖 + google + 參考系統源碼,自己寫了個ContentProvider + ContentObserver + SQLiteDatabase的例子
感謝大V
(由於還沒接收到驗證郵件,不能發日志,就發壇子裡了,獻丑了...)
- package lab.sodino.broadcastaction;
- import lab.sodino.util.DatabaSEOpenHelper;
- import lab.sodino.util.SodinoOut;
- import android.app.Activity;
- import android.content.ContentResolver;
- import android.database.ContentObserver;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.VIEw;
- import android.view.VIEwGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.ScrollVIEw;
- import android.widget.TextVIEw;
- /**
- * 本例子將記錄可靜態注冊的廣播被監聽到的頻度。<br/>
- * 1.建立一表{ACTION_NAME廣播名稱,LAST_TIME最近一次發生時間,COUNT總共記錄到的次數}<br/>
- * 2.在ActionReceiver中監聽廣播,並記錄。 <br/>
- * 3.在DBContentProvider中更新數據庫記錄<br/>
- * 4.在BroadcastActionRecordAct.ActionDBObserver中監聽數據庫的變化,
- * 並使用Handler機制將最新情況顯示在txtInfo上。<br/>
- * 5.DatabaSEOpenHelper將實現基本的數據庫操作。
- *
- * @author Sodino
- */
- public class BroadcastActionRecordAct extends Activity implements
- Button.OnClickListener {
- private TextVIEw txtInfo;
- private DatabaSEOpenHelper dbHelper;
- private Button btnRefresh;
- /** clear功能未完善。 */
- private Button btnClear;
- private Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- String info = (String) msg.obj;
- txtInfo.setText(info);
- }
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- LayoutParams lpPC = new LayoutParams(LayoutParams.FILL_PARENT,
- LayoutParams.WRAP_CONTENT);
- LayoutParams lpCC = new LayoutParams(LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT);
- btnRefresh = new Button(this);
- btnRefresh.setLayoutParams(lpCC);
- btnRefresh.setText("Refresh");
- btnRefresh.setOnClickListener(this);
- btnClear = new Button(this);
- btnClear.setLayoutParams(lpCC);
- btnClear.setText("ClearTable");
- btnClear.setOnClickListener(this);
- LinearLayout subLayout = new LinearLayout(this);
- subLayout.setLayoutParams(lpPC);
- subLayout.setOrIEntation(LinearLayout.HORIZONTAL);
- subLayout.addVIEw(btnRefresh);
- subLayout.addVIEw(btnClear);
- txtInfo = new TextVIEw(this);
- txtInfo.setLayoutParams(lpPC);
- txtInfo.setTextColor(0xff0000ff);
- txtInfo.setBackgroundColor(0xffffffff);
- txtInfo.setText("Starting...");
- txtInfo.setTextSize(15);
- ScrollView scrollView = new ScrollVIEw(this);
- scrollVIEw.setLayoutParams(lpPC);
- scrollView.addVIEw(txtInfo);
- LinearLayout mainLayout = new LinearLayout(this);
- mainLayout.setLayoutParams(lpPC);
- mainLayout.setOrIEntation(LinearLayout.VERTICAL);
- mainLayout.addVIEw(subLayout);
- mainLayout.addView(scrollVIEw);
- setContentVIEw(mainLayout);
- dbHelper = new DatabaSEOpenHelper(this);
- ContentResolver contentResolver = getContentResolver();
- contentResolver.registerContentObserver(DBContentProvider.CONTENT_URI,
- false, new ActionDBObserver(handler));
- }
- public void onClick(View vIEw) {
- if (vIEw == btnRefresh) {
- refreshRecord();
- } else if (vIEw == btnClear) {
- clearRecord();
- }
- }
- public void refreshRecord() {
- dbHelper.openReadableDatabase();
- String info = dbHelper.getAllOrderedList(DatabaSEOpenHelper.DESC);
- dbHelper.close();
- if (info != null) {
- txtInfo.setText(info);
- } else {
- txtInfo.setText("<NULL/>");
- }
- dbHelper.close();
- }
- public void clearRecord() {
- dbHelper.openWritableDatabase();
- dbHelper.clearRecord();
- dbHelper.close();
- }
- private class ActionDBObserver extends ContentObserver {
- private Handler handler;
- public ActionDBObserver(Handler handler) {
- super(handler);
- this.handler = handler;
- }
- public void onChange(boolean selfChange) {
- super.onChange(selfChange);
- String[] projection = { "ACTION_NAME", "LAST_TIME", "COUNT" };
- // String selection = "select * from ActionTable";
- String sortOrder = "COUNT DESC";
- // dbHelper.openReadableDatabase();
- // Cursor cursor = dbHelper.query(projection, null, null,
- // sortOrder);
- Cursor cursor = managedQuery(DBContentProvider.CONTENT_URI,
- projection, null, null, sortOrder);
- String info = "";
- String line = "";
- int actionIdx = 0;
- int timeIdx = 1;
- int countIdx = 2;
- while (cursor.moveToNext()) {
- line += cursor.getString(actionIdx) + " ";
- line += cursor.getString(timeIdx) + " ";
- line += cursor.getString(countIdx) + "\n";
- info += line;
- line = "";
- }
- Message msg = new Message();
- msg.obj = info;
- handler.sendMessage(msg);
- cursor.close();
- // dbHelper.close();
- SodinoOut.out("Database does changed!!!");
- }
- public boolean deliverSelfNotifications() {
- return super.deliverSelfNotifications();
- }
- }
- }