Android实现静态广播监听器的方法实现静态广播监听器的方法
主要介绍了Android实现静态广播监听器的方法,涉及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;
/**
* 本例子将记录可静态注册的广播被监听到的频度。<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);
评论0