服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现静态广播监听器的方法

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

2021-03-27 15:39鉴客 Android

这篇文章主要介绍了Android实现静态广播监听器的方法,涉及Android的广播机制与记录监听广播信息的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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程序设计有所帮助。

延伸 · 阅读

精彩推荐