Android: Dynamic Registration Broadcastreceiver

Source: Internet
Author: User

Comments are directly written in the code, more intuitive, I think these definitions of the system's broadcast macro, not very good;

Mainactivity.java:

 PackageCom.vrinux.broadcastdemo;Importandroid.app.Activity;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.widget.Toast; Public  class mainactivity extends Activity {    //define a filter;    PrivateIntentfilter Intentfilter;//define a broadcast listener;    PrivateNetchangreceiver Netchangreceiver;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.fragment_main);//instantiate filter;Intentfilter =NewIntentfilter ();//Add a filtered action value;Intentfilter.addaction (intent.action_airplane_mode_changed);//instantiation of the broadcast listener;Netchangreceiver =NewNetchangreceiver ();//The broadcast Listener and filter are registered together;Registerreceiver (Netchangreceiver, Intentfilter); }@Override    protected void OnDestroy() {//TODO auto-generated method stub        Super. OnDestroy ();//Cancel registration of the broadcast listener while destroying activity;Unregisterreceiver (Netchangreceiver); }//Create a broadcast listener that inherits Broadcastreceiver;Class Netchangreceiver extends Broadcastreceiver {//Override the OnReceive method, the entity of the method, to receive the broadcast execution code;        @Override         Public void OnReceive(context context, Intent Intent) {//TODO auto-generated method stubLOG.I ("Mainactivity","Airplane mode start ~"); Toast.maketext (mainactivity. This,"Airplane mode start ~", Toast.length_short). Show (); }    }}

Android: Dynamic Registration Broadcastreceiver

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.