Android Radio (Broacastreceiver) and services (service)

Source: Internet
Author: User

Broadcastreceiver can be understood as a component, is silently in the background to run, used in different software and different components of the transmission, can not be perceived by the user, because he works inside the system, Broadcastreceiver is called broadcast.

There are two ways to register a broadcast:

1, dynamic registration, using Java code to register

2, static registration, need to register in Androidmainfest

Second, we will first explain how to register static:

1, we first create a class that inherits Broadcastreceiver, implements the OnReceive method, for a better experience, we play a toast in this method

OnReceive will trigger after receiving the broadcast.

1  Public classMsgserviceextendsBroadcastreceiver {2 3     PrivateString ACTION_SGC = "Android.intent.action.BOOT_COMPleTED";4 5 @Override6      Public voidOnReceive (Context context, Intent Intent) {7         if(Action_sgc.equals (Intent.getaction ())) {8Toast.maketext (Context, "static broadcast", Toast.length_short). Show ();9         }Ten     } One}

2. This is required to register in Androidmainfest

1 <uses-permissionAndroid:name= "Android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>2     3     <Application4         Android:allowbackup= "true"5 Android:icon= "@mipmap/ic_launcher"6 Android:label= "@string/app_name"7 Android:roundicon= "@mipmap/ic_launcher_round"8 Android:supportsrtl= "true"9 Android:theme= "@style/apptheme">Ten         <ActivityAndroid:name=". Mainactivity "> One             <Intent-filter> A                 <ActionAndroid:name= "Android.intent.action.MAIN" /> -  -                 <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> the             </Intent-filter> -         </Activity> -         <receiverAndroid:name=". Msgservice "> -             <Intent-filter> +                 <ActionAndroid:name= "Android.intent.action.BOOT_COMPleTED"></Action> -             </Intent-filter> +         </receiver> A     </Application>

The registered code is 17 lines to 20 lines, do not forget to write permissions, permissions are the first row of permissions

3. How to use the broadcast

1  Public classMainactivityextendsappcompatactivity {2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7 init ();8     }9 Ten     Private voidinit () { OneFindviewbyid (R.id.button). Setonclicklistener (NewView.onclicklistener () { A @Override -              Public voidOnClick (View v) { -                 //the intention here must be to use the same intent as the other Android.intent.action.BOOT_COMPleTED if the use is different, an error will occur theSendbroadcast (NewIntent ("Android.intent.action.BOOT_COMPleTED")); -             } -         }); -     } +}

      

  

  

Android Radio (Broacastreceiver) and services (service)

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.