An example analysis of BroadcastReceiver developed by Android

  • 2020-06-19 11:42:43
  • OfStack

This article illustrates BroadcastReceiver usage in Android development. Share to everybody for everybody reference. The specific analysis is as follows:

In Android systems, broadcast (Broadcast) is a mechanism for propagating data between components (Intent).

Braodcast Receiver, as the name suggests, is a broadcast receiver and is similar to an event handling mechanism, but the event handling mechanism is at the program component level (for example, a button click event) while the broadcast event handling mechanism is at the system level. We can use Intent to start a component or sendBroadcast() to start a system-level event broadcast to deliver messages. We can also implement Broadcast Receiver in our own applications to listen and respond to broadcast Intent.

The broadcast of the event is emitted by creating the Intent object and calling the sendBroadcast() method. Event acceptance is achieved by defining a class that inherits from BroadcastReceiver and overrides its onReceive() method in response to the event.

Below is Broadcast Action, which defines many of the standards in the android system to respond to the broadcast events of the system.

ACTION_TIME_CHANGED (triggered when time changes)
ACTION_BOOT_COMPLETED(triggered after system startup)-- for example, some programs start after startup in this way
ACTION_PACKAGE_ADDED (triggered when package is added)
ACTION_BATTERY_CHANGED (triggered when power is low)

Here's an example:

We bind an event to a button that triggers logcat to hit an log by sending a broadcast.

Look at the manifest file first.


<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  package="com.example.test" 
  android:versionCode="1" 
  android:versionName="1.0" > 
  <uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="16" />
  <application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" >
    <activity 
      android:name="com.example.broadcast.BroadcastReceiverActivity" 
      android:label="@string/app_name_bc" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" >
        </action>
        <category android:name="android.intent.category.LAUNCHER" >
        </category>
      </intent-filter>
    </activity>
    <receiver android:name="com.example.broadcast.HelloBroadReciever" >
      <intent-filter>
        <action android:name="comz.test.printlog" >
        </action>
      </intent-filter>
    </receiver>
  </application>
</manifest>

One receiver is declared above. Receive a message with the name comz.test.printlog.

See activity:


package com.example.broadcast;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.example.test.R;
public class BroadcastReceiverActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b1 = (Button) findViewById(R.id.broadcastBtn);
    b1.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Log.e("mason", "here");
        //  define 1 a intent
        Intent intent = new Intent().setAction("comz.test.printlog")
            .putExtra("info", "here is your info.");
        //  The broadcast went out 
        sendBroadcast(intent);
      }
    });
  }
}

In this code, define an intent and broadcast it.

Look at the BroadReceiver code:


package com.example.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class HelloBroadReciever extends BroadcastReceiver {
  //  If the received event occurs 
  @Override
  public void onReceive(Context context, Intent intent) {
    Log.e("mason", "on receive");
    if (intent.getAction().equals("comz.test.printlog")) {
      Log.e("mason", intent.getStringExtra("info"));
    }
  }
}

This is the code for BroadcastReceiver.

After receiving the message, if it is comz. test. printlog, print the message.

I hope this article has been helpful for your Android programming.


Related articles: