Case Explanation of BroadcastReceiver in Android

  • 2021-11-02 02:43:56
  • OfStack

Preface

When we register an App, if the registration is successful, then we will skip the login interface directly and enter the main interface directly, then we will realize this function through BroadcastReceiver now:

(1) Register interface RegisterActivity. java, where some basic operations are skipped and the registration method is called directly signUp() Methods:


btn_reg.signUp(RegisterActivity.this, new SaveListener() {
      @Override
      public void onSuccess() {
        progress.dismiss();
        ShowToast(" Successful registration ");
        /**  If the registration is successful, send a broadcast notification to log in to exit the page   **/
        sendBroadcast(new Intent(Constants.ACTION_REGISTER_SUCCESS_FINISH));
        //  Start the main page 
        Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
        startActivity(intent);
        finish();
      }
      @Override
      public void onFailure(int arg0, String arg1) {
        BmobLog.i(arg1);
        ShowToast(" Registration failed :" + arg1);
        progress.dismiss();
      }
    });

We can see that one broadcast has been sent with the name: Constants.ACTION_REGISTER_SUCCESS_FINISH)

(2) How to notify the login interface? The following is the implementation of the login interface:

We need to be in the login interface onCreate Method to dynamically register 1 broadcast recipient:


@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    // Dynamic registration 1 Broadcast recipients 
    // Sign up to exit the broadcast: Yes 1 At the beginning of registration, if the registration is successful, you will exit the landing interface and directly enter the main interface 
    IntentFilter filter = new IntentFilter();
    //  After successful registration, the landing page exits 
    // public static final String ACTION_REGISTER_SUCCESS_FINISH ="register.success.finish";
    filter.addAction(BmobConstants.ACTION_REGISTER_SUCCESS_FINISH); 
    registerReceiver(receiver, filter); // Register in 
  }

It is also worth noting that we have dynamically registered haunted for 1 broadcast recipient, and we need to onDestroy() Contact binding in method:


@Override
  protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(receiver);
  }

(3) The following is a class of broadcast recipients defined by yourself. When matching intentions, the login interface finish:


  public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
      // If there is a match, it executes finnish()
      if (intent != null && BmobConstants.ACTION_REGISTER_SUCCESS_FINISH.equals(intent.getAction())) {
        finish(); // After successful registration, you will log in to the interface finish()
      }
    }
  }

Summarize


Related articles: