Android实现静态广播监听器的方法
本文实例讲述了Android实现静态广播监听器的方法。分享给大家供大家参考。具体实现方法如下:
packagelab.sodino.broadcastaction;
importlab.sodino.util.DatabaseOpenHelper;
importlab.sodino.util.SodinoOut;
importandroid.app.Activity;
importandroid.content.ContentResolver;
importandroid.database.ContentObserver;
importandroid.database.Cursor;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.view.View;
importandroid.view.ViewGroup.LayoutParams;
importandroid.widget.Button;
importandroid.widget.LinearLayout;
importandroid.widget.ScrollView;
importandroid.widget.TextView;
/**
*本例子将记录可静态注册的广播被监听到的频度。<br/>
*1.建立一表{ACTION_NAME广播名称,LAST_TIME最近一次发生时间,COUNT总共记录到的次数}<br/>
*2.在ActionReceiver中监听广播,并记录。<br/>
*3.在DBContentProvider中更新数据库记录<br/>
*4.在BroadcastActionRecordAct.ActionDBObserver中监听数据库的变化,
*并使用Handler机制将最新情况显示在txtInfo上。<br/>
*5.DatabaseOpenHelper将实现基本的数据库操作。
*
*@authorSodino
*/
publicclassBroadcastActionRecordActextendsActivityimplements
Button.OnClickListener{
privateTextViewtxtInfo;
privateDatabaseOpenHelperdbHelper;
privateButtonbtnRefresh;
/**clear功能未完善。*/
privateButtonbtnClear;
privateHandlerhandler=newHandler(){
publicvoidhandleMessage(Messagemsg){
Stringinfo=(String)msg.obj;
txtInfo.setText(info);
}
};
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
LayoutParamslpPC=newLayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
LayoutParamslpCC=newLayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
btnRefresh=newButton(this);
btnRefresh.setLayoutParams(lpCC);
btnRefresh.setText("Refresh");
btnRefresh.setOnClickListener(this);
btnClear=newButton(this);
btnClear.setLayoutParams(lpCC);
btnClear.setText("ClearTable");
btnClear.setOnClickListener(this);
LinearLayoutsubLayout=newLinearLayout(this);
subLayout.setLayoutParams(lpPC);
subLayout.setOrientation(LinearLayout.HORIZONTAL);
subLayout.addView(btnRefresh);
subLayout.addView(btnClear);
txtInfo=newTextView(this);
txtInfo.setLayoutParams(lpPC);
txtInfo.setTextColor(0xff0000ff);
txtInfo.setBackgroundColor(0xffffffff);
txtInfo.setText("Starting...");
txtInfo.setTextSize(15);
ScrollViewscrollView=newScrollView(this);
scrollView.setLayoutParams(lpPC);
scrollView.addView(txtInfo);
LinearLayoutmainLayout=newLinearLayout(this);
mainLayout.setLayoutParams(lpPC);
mainLayout.setOrientation(LinearLayout.VERTICAL);
mainLayout.addView(subLayout);
mainLayout.addView(scrollView);
setContentView(mainLayout);
dbHelper=newDatabaseOpenHelper(this);
ContentResolvercontentResolver=getContentResolver();
contentResolver.registerContentObserver(DBContentProvider.CONTENT_URI,
false,newActionDBObserver(handler));
}
publicvoidonClick(Viewview){
if(view==btnRefresh){
refreshRecord();
}elseif(view==btnClear){
clearRecord();
}
}
publicvoidrefreshRecord(){
dbHelper.openReadableDatabase();
Stringinfo=dbHelper.getAllOrderedList(DatabaseOpenHelper.DESC);
dbHelper.close();
if(info!=null){
txtInfo.setText(info);
}else{
txtInfo.setText("<NULL/>");
}
dbHelper.close();
}
publicvoidclearRecord(){
dbHelper.openWritableDatabase();
dbHelper.clearRecord();
dbHelper.close();
}
privateclassActionDBObserverextendsContentObserver{
privateHandlerhandler;
publicActionDBObserver(Handlerhandler){
super(handler);
this.handler=handler;
}
publicvoidonChange(booleanselfChange){
super.onChange(selfChange);
String[]projection={"ACTION_NAME","LAST_TIME","COUNT"};
//Stringselection="select*fromActionTable";
StringsortOrder="COUNTDESC";
//dbHelper.openReadableDatabase();
//Cursorcursor=dbHelper.query(projection,null,null,
//sortOrder);
Cursorcursor=managedQuery(DBContentProvider.CONTENT_URI,
projection,null,null,sortOrder);
Stringinfo="";
Stringline="";
intactionIdx=0;
inttimeIdx=1;
intcountIdx=2;
while(cursor.moveToNext()){
line+=cursor.getString(actionIdx)+"";
line+=cursor.getString(timeIdx)+"";
line+=cursor.getString(countIdx)+"/n";
info+=line;
line="";
}
Messagemsg=newMessage();
msg.obj=info;
handler.sendMessage(msg);
cursor.close();
//dbHelper.close();
SodinoOut.out("Databasedoeschanged!!!");
}
publicbooleandeliverSelfNotifications(){
returnsuper.deliverSelfNotifications();
}
}
}
希望本文所述对大家的Android程序设计有所帮助。