SoFunction
Updated on 2025-04-09

How to implement static broadcast listener on Android

How to implement static broadcast listener on Android

Updated: July 14, 2015 16:55:59 Author: Jianke
This article mainly introduces the method of Android to implement static broadcast monitors, involving Android's broadcast mechanism and related skills for recording and monitoring broadcast information. It has certain reference value. Friends who need it can refer to it.

This article describes the method of implementing static broadcast listeners on Android. Share it for your reference. The specific implementation method is as follows:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
  * This example will record the frequency at which a statically registered broadcast is heard.  <br/>
  * 1. Create a table {ACTION_NAME broadcast name, LAST_TIME latest occurrence time, total number of times COUNT recorded}<br/>
  * 2. Listen to the broadcast in ActionReceiver and record it.  <br/>
  * 3. Update database records in DBContentProvider<br/>
  * 4. Listen to the changes in the database in the middle,
  * And use the Handler mechanism to display the latest situation on txtInfo.  <br/>
  * Basic database operations will be implemented.
  *
  * @author Sodino
  */
public class BroadcastActionRecordAct extends Activity implements
   {
 private TextView txtInfo;
 private DatabaseOpenHelper dbHelper;
 private Button btnRefresh;
 /** clear function is not perfect.  */
 private Button btnClear;
 private Handler handler = new Handler() {
  public void handleMessage(Message msg) {
   String info = (String) ;
   (info);
  }
 };
 @Override
 public void onCreate(Bundle savedInstanceState) {
  (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);
  (lpCC);
  ("Refresh");
  (this);
  btnClear = new Button(this);
  (lpCC);
  ("ClearTable");
  (this);
  LinearLayout subLayout = new LinearLayout(this);
  (lpPC);
  ();
  (btnRefresh);
  (btnClear);
  txtInfo = new TextView(this);
  (lpPC);
  (0xff0000ff);
  (0xffffffff);
  ("Starting...");
  (15);
  ScrollView scrollView = new ScrollView(this);
  (lpPC);
  (txtInfo);
  LinearLayout mainLayout = new LinearLayout(this);
  (lpPC);
  ();
  (subLayout);
  (scrollView);
  setContentView(mainLayout);
  dbHelper = new DatabaseOpenHelper(this);
  ContentResolver contentResolver = getContentResolver();
  (DBContentProvider.CONTENT_URI,
    false, new ActionDBObserver(handler));
 }
 public void onClick(View view) {
  if (view == btnRefresh) {
   refreshRecord();
  } else if (view == btnClear) {
   clearRecord();
  }
 }
 public void refreshRecord() {
  ();
  String info = ();
  ();
  if (info != null) {
   (info);
  } else {
   ("&lt;NULL/&gt;");
  }
  ();
 }
 public void clearRecord() {
  ();
  ();
  ();
 }
 private class ActionDBObserver extends ContentObserver {
  private Handler handler;
  public ActionDBObserver(Handler handler) {
   super(handler);
    = handler;
  }
  public void onChange(boolean selfChange) {
   (selfChange);
   String[] projection = { "ACTION_NAME", "LAST_TIME", "COUNT" };
   // String selection = "select * from ActionTable";
   String sortOrder = "COUNT DESC";
   // ();
   // Cursor cursor = (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 (()) {
    line += (actionIdx) + " ";
    line += (timeIdx) + " ";
    line += (countIdx) + "/n";
    info += line;
    line = "";
   }
   Message msg = new Message();
    = info;
   (msg);
   ();
   // ();
   ("Database does changed!!!");
  }
  public boolean deliverSelfNotifications() {
   return ();
  }
 }
}

I hope this article will be helpful to everyone's Android programming design.

  • Android
  • broadcast
  • monitor

Related Articles

  • Android implements image compression (six compression methods of bitmap)

    Pictures in Android exist in the form of bitmap. This article mainly introduces Android to implement image compression (six compression methods of bitmap). If you are interested, you can learn about it.
    2017-02-02
  • A brief analysis of Android file manager (project 1)

    This article mainly introduces the relevant information of the Android file manager (I). Friends who need it can refer to it.
    2015-11-11
  • Detailed explanation of the implementation of local cache of Flutter network pictures

    This article mainly introduces a detailed analysis of the implementation example of Flutter network image local cache. Friends in need can refer to it for reference. I hope it can be helpful. I wish you more progress and get promoted as soon as possible to get a salary increase as soon as possible.
    2023-04-04
  • Android implementation converts View into pictures and saves them locally

    This article mainly introduces in detail to Android implementation to convert View into pictures and save them locally. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it.
    2022-02-02
  • AppWidget tutorial in Android

    This article mainly introduces the introduction tutorial for AppWidget in Android. This article explains how to create a simple AppWidget, how to make AppWidget interact with client programs, etc., friends who need it can refer to it
    2015-03-03
  • Android custom SMS countdown view process analysis

    There are three ways to implement the countdown, and this custom view is implemented through handler. This article introduces the Android custom SMS countdown view process through example code. Friends who need it can refer to it.
    2020-03-03
  • Android permission settings and self-start settings

    Today, the editor will share with you an article on Android's permission settings and self-start settings. It has good reference value and hope it will be helpful to everyone. Let's take a look with the editor
    2018-07-07
  • Android development for creating, using and encapsulation operations

    This article mainly introduces the creation, use and encapsulation operations of buffered dialog boxes in Android development. It analyzes the implementation methods of creating, setting, display, closing and other operations of Android buffered dialog boxes based on specific examples. Friends who need it can refer to it.
    2017-09-09
  • Android Studio package to generate APK file

    Android Studio is Google launching an Android integrated development tool based on IntelliJ IDEA. This article mainly introduces the method of Android Studio packaging and generating APK files. Friends who need it can refer to it.
    2018-07-07
  • Based on Android, save pictures locally and display them in the album.

    App applications are becoming more and more user-friendly, not only have beautiful interfaces but also have many diverse services and are very convenient to operate. Through this article, I will introduce to you the Android-based image saving locally and can be displayed in the album. Friends who are interested in the knowledge about saving images on Android will learn together.
    2015-12-12

Latest Comments