Details of Android Broadcast receiving Mechanism (with short Message receiving implementation)

  • 2020-06-23 01:55:11
  • OfStack

A detailed explanation of broadcasting in Android.

1. Registration process of BroadcastReceiver:
(1). After the broadcast message is issued, only the subscribed object will receive the broadcast message and make corresponding processing.
**(2).**Android broadcasting is divided into two aspects: broadcast sender and broadcast receiver. Broadcast in Android USES the observer pattern, a message-based publish/subscribe event model. The receiver registers with AMS via the Binder mechanism. AMS looks for Broadcastreceiver that meets the criteria and sends the broadcast to the corresponding message loop queue of BroadcastReceiver(1 in general, Activity). The message loop executes to this broadcast, calling back the onReceive() method in BroadcastReceiver.
(3) The order of execution of the broadcast sender and the broadcast receiver is asynchronous, and the outgoing broadcast does not care whether the receiver has received the broadcast or not, and it is uncertain when the receiver will receive the broadcast.
2. Steps of BroadcastReceiver
(1). Register broadcast: The receiver registers broadcast with AMS.
(2). Broadcast sender: Broadcast sender sends a broadcast to AMS.
(3). Receive broadcast: After the receiver gets the broadcast, it calls onReceive() method to execute.

Here is an example of sending a text message. The code is as follows:


BroadcastReceiverHelper  Class: 

package com.scd.broadcastreceiver.helper;

import com.scd.broadcastreceiver.activity.MainActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
 *  Radio reception 
 * 
 * @author scd
 * 
 */
public class BroadcastReceiverHelper extends BroadcastReceiver {
  /**  context  */
  private MainActivity mActivity = null;

  public BroadcastReceiverHelper(MainActivity mContext) {
    super();
    mActivity = mContext;
  }

  @Override
  public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(MainActivity.ACTION_SENDMESSAGE)) {
      toast(" Send a success ");
    } else if (intent.getAction()
        .equals(MainActivity.ACTION_DELIVERMESSAGE)) {
      toast(" Receive the success ");
    }

  }

  public void toast(String text) {
    Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show();

  }

}


MainActivity class:


package com.scd.broadcastreceiver.activity;

import com.scd.broadcastreceiver.R;
import com.scd.broadcastreceiver.helper.BroadcastReceiverHelper;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
  private Button mButton = null;
  /**  Send a text message  */
  public static String ACTION_SENDMESSAGE = "com.scd.broadcastreceiver.MainActivity.SendMessage";
  /**  Receive SMS  */
  public static String ACTION_DELIVERMESSAGE = "com.scd.broadcastreceiver.MainActivity.DeliverMessage";
  /** Reciver class  */
  private BroadcastReceiverHelper mHelperS = null;
  private BroadcastReceiverHelper mHelperD = null;
  /**  SMS management  */
  private SmsManager mSmsManager = null;

  /**  The phone number  */
  private String mTelephone = "13607567010";

  /**  Message content  */
  private String mContext = " Hello, can I help you? ";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mButton = (Button) findViewById(R.id.sendBroadcast);
    mButton.setOnClickListener(this);
    mSmsManager = SmsManager.getDefault();
    //  Registration of radio 
    registerBroadcast();
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.sendBroadcast: {
      // Send broadcast 
      sendBroadcastReceiver(mTelephone, mContext);
      break;
    }

    default:
      break;
    }

  }

  /**
   *  Registration of radio 
   */
  public void registerBroadcast() {
    //  send 
    IntentFilter intentFilterS = new IntentFilter(ACTION_SENDMESSAGE);
    mHelperS = new BroadcastReceiverHelper(MainActivity.this);
    this.registerReceiver(mHelperS, intentFilterS);
    //  receive 
    IntentFilter intentFilterD = new IntentFilter(ACTION_DELIVERMESSAGE);
    mHelperD = new BroadcastReceiverHelper(MainActivity.this);
    this.registerReceiver(mHelperD, intentFilterD);
  }

  /**
   *  Send broadcast   The process of sending SMS: When sending SMS, you need to send broadcast 1 Second, the receiver needs to send a broadcast 1 time 
   */
  public void sendBroadcastReceiver(String telephone, String content) {
    if (telephone != null) {
      Intent sIntent = new Intent(ACTION_SENDMESSAGE);
      //  Send the broadcast only after the text message has been successfully sent 
      PendingIntent sentIntent = PendingIntent.getBroadcast(
          MainActivity.this, 0, sIntent, 0);

      Intent dIntent = new Intent(ACTION_DELIVERMESSAGE);
      //  The broadcast is not sent until the message is received 
      PendingIntent deliveryIntent = PendingIntent.getBroadcast(
          MainActivity.this, 0, dIntent, 0);
      mSmsManager.sendTextMessage(telephone, null, content, sentIntent,
          deliveryIntent);
      // this.sendBroadcast(intent);

    }

  }
}

Note: The following permissions need to be added:


 <!--  SMS privileges  -->
  <uses-permission android:name="android.permission.SEND_SMS" >
  </uses-permission>
  <uses-permission android:name="android.permission.READ_SMS" >
  </uses-permission>
  <uses-permission android:name="android.permission.RECEIVE_SMS" >
  </uses-permission>


Related articles: