The example in this article describes the way Android implements static broadcast listeners. Share to everyone for your reference. The implementation method is as follows:
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; /** * This example will record how often a static registered broadcast can be heard. <br/> * 1. Create a Table {Action_name broadcast name, last_time the last occurrence, the number of times that count was recorded}<br/> * 2. Monitor broadcasts in Actionreceiver and record. <br/> * 3. Update database records in Dbcontentprovider <br/> * 4. Monitor the changes in the database in the Broadcastactionrecordact.actiondbobserver, * and use the handler mechanism to display the latest situation on the txtinfo.
<br/> * 5.DatabaseOpenHelper will implement basic database operations. * * @author Sodino */public class Broadcastactionrecordact extends activity implements Button.onclicklistener {PRI Vate TextView Txtinfo;
Private Databaseopenhelper DBHelper;
Private Button Btnrefresh; The/** clear feature is not complete.
* * Private Button btnclear;
Private Handler Handler = new Handler () {public void Handlemessage (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 = "";
msg = new Message ();
Msg.obj = info;
Handler.sendmessage (msg);
Cursor.close ();
Dbhelper.close ();
Sodinoout.out ("Database does changed!!!");
public Boolean deliverselfnotifications () {return super.deliverselfnotifications ();
}
}
}
I hope this article will help you with your Android program.