栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

本文实例讲述了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;

public class BroadcastActionRecordAct extends Activity implements
  Button.onClickListener {
 private TextView txtInfo;
 private DatabaseOpenHelper dbHelper;
 private Button btnRefresh;
 
 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("");
  }
  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程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/164304.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号