Android Studio Learn (iii) broadcast

Source: Internet
Author: User

Dynamic Registration monitoring Network changes
    • Create Intentfilter and Addaction represents which broadcast to listen on
    • Then use the Registerreceiver () method to pass the Intentfilter with the listener you created.
    • Need for dynamic registration Unregisterreciver ()
    • Permissions need to be declared in Mainfest <uses-permission android:name "Android.permission.ACCESS_NETWORK. State "/>

Public class Mainactivity extends Appcompatactivity {
Private intentfilter intentfilter;
Private Networkchangedreceiver Networkchangedreceiver;
@Override
protected void onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Intentfilter = new Intentfilter ();
Intentfilter.addaction ("Android.net.conn.CONNECTIVITY_CHANGE");
Networkchangedreceiver = new Networkchangedreceiver ();
Registerreceiver (Networkchangedreceiver,intentfilter);
}

@Overrideprotected void onDestroy() {    super.onDestroy();    unregisterReceiver(networkChangedReceiver);}class NetworkChangedReceiver extends BroadcastReceiver{    @Override    public void onReceive (Context context,Intent intent)    {        ConnectivityManager connectivityManager =                (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();        if(networkInfo!=null && networkInfo.isAvailable())        {            Toast.makeText(context,"good",Toast.LENGTH_LONG).show();        }        else        {            Toast.makeText(context,"bad",Toast.LENGTH_LONG).show();        }    }}

}

Static registration for boot start
    • new-> other->broadcast Receiver Manually create a broadcast
    • Exported allow this broadcast to receive broadcasts outside this program

Then declare the permissions in Mainfest
and indicate the broadcast to be monitored.

Send broadcast
    • Intent Intent = new Intent ("com ....")
    • Sendbroadcast (Intent);
    • Sendorderedbroadcast (Intent,null); Send an ordered broadcast

    • <intent-filter android:p riority= ">//" Set broadcast priority
    • Abortbroadcast () truncated broadcast

Local broadcast

public class Mainactivity extends Appcompatactivity {
Private Intentfilter Intentfilter;
Private Localreceiver Localreceiver;
Private Localbroadcastmanager Localbroadcastmanager;

@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.activity_main);    Get an instance of the local broadcast manager Localbroadcastmanager = Localbroadcastmanager.getinstance (this);    Dynamically registering the local broadcast receiver Intentfilter = new Intentfilter ();    Intentfilter.addaction ("Com.example.broadcasttest.LOCAL_BROADCAST");    Localreceiver = new Localreceiver ();    Localbroadcastmanager.registerreceiver (Localreceiver, Intentfilter);    Send local Broadcast button button = (button) Findviewbyid (R.id.button); Button.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {in            Tent Intent = new Intent ("Com.example.broadcasttest.LOCAL_BROADCAST");        Localbroadcastmanager.sendbroadcast (Intent); }    });}    @Overrideprotected void OnDestroy () {Super.ondestroy (); Cancels the registration of the dynamic network change broadcast receiver Localbroadcastmanager.unregisterreceiver (localreceiver);} Class Localreceiver extends Broadcastreceiver {@OvErride public void OnReceive (context context, Intent Intent) {toast.maketext (context, "received local broadcast    ", Toast.length_short). Show (); }}

}

Android Studio Learn (iii) broadcast

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.