欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现静态广播监听器的方法

程序员文章站 2023-11-08 14:35:10
本文实例讲述了android实现静态广播监听器的方法。分享给大家供大家参考。具体实现方法如下: package lab.sodino.broadcastactio...

本文实例讲述了android实现静态广播监听器的方法。分享给大家供大家参考。具体实现方法如下:

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();
  }
 }
}

希望本文所述对大家的android程序设计有所帮助。