Detailed broadcast mechanism, broadcast receiver,android advanced Prerequisite Knowledge (I.).

Source: Internet
Author: User

Straight to the subject, some definitions of the broadcast I think as long as it is a qualified primary Android development This all know, I have to do is how to use broadcast receiver.

1. How to listen to the system broadcast?

Android system has many built-in system broadcasts: Phone, SMS, time, region, battery level, network change, etc... So how do we implement monitoring in our applications? Below we listen to monitor the mobile network changes in the broadcast, we all know that there are two ways to register the broadcast, where I use the code to register the way, in order to better understand the broadcasting operation mechanism. In the code to register must pay attention to when you use the completion of must be anti-registration, must be anti-registration, must be anti-registration off. Here's a look at the code:

 Public classMainactivityextendsactivity{PrivateIntentfilter Intentfilter; PrivateNetworkchangereceiver Networkchangereceiver; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Intentfilter=NewIntentfilter (); Intentfilter.addaction ("Android.net.conn.CONNECTIVITY_CHANGE"); Networkchangereceiver=NewNetworkchangereceiver ();    Registerreceiver (Networkchangereceiver,intentfilter); }    classNetworkchangereceiverextendsbroadcastreceiver{@Override Public voidOnReceive (Context context, Intent Intent) {Connectivitymanager ConnectionManager=(Connectivitymanager) Getsystemservice (Context.connectivity_service); Networkinfo Networkinfo=Connectionmanager.getactivenetworkinfo (); if(Networkinfo! =NULL&&networkinfo.isavailable ()) {Toast.maketext (context,"Network is available", Toast.length_short). Show (); }Else{toast.maketext (context,"Network is unavailable", Toast.length_short). Show (); } }} @Overrideprotected voidOnDestroy () {Super. OnDestroy ();    Unregisterreceiver (Networkchangereceiver); }}

Here I have implemented the monitoring of the network, monitoring whether the current network is available. Android system in order to ensure the security of the program to query the network status must require permissions, so I registered the permissions in the manifest file:

<android:name= "Android.permission.ACCESS_NETWORK_STATE"></ uses-permission >

This app you can copy down to run.

2. Static registration?

The above is used for dynamic registration, mainly to allow us to more clearly understand the operation of the broadcasting mechanism. But there is no denying the convenience of static registration. Dynamic registration allows us to manage broadcast registration and logout very well, very flexible but one of the fatal drawbacks is that we must be able to use this broadcast receiver after the application is launched. Static registration just makes up for this, and when you don't start the program, you can listen to the broadcast (which is why a lot of malware is using this method to pit you, but you know that no application will start its own operation in a situation where you haven't started it at all.) The simple point is that if you come down with a virus, but you do not open it once, then it will not be harmful to you). The following examples still speak:

The following implementation of monitoring the start-up broadcast program, do not forget the permissions problem, the first in the list to add permissions:

<android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"></ uses-permission >

Create our receivers below:

 Public class extends broadcastreceiver {    @Override    publicvoid  onreceive (Context Context, Intent Intent) {        Toast.maketext (context,"Start successful", Toast.length_short). Show ();    }}

Here the code is very simple, just need to do our logical processing, mainly in the manifest file registration

 <  receiver  android:name  = ". Bootcompletereceiver " >  <  intent-filter                 >  <  action  android:name  = "Android.intent.action.BOOT_COMPLETED"             />  </ intent-filter         >  </ receiver  >  

Okay, I'm sure you read these two examples already understand the broadcasting mechanism, in the example we just pop up a few toast, you can do your own logic in the operation.

Know the definition of the broadcast, stating that you are getting started.

When you understand these two examples and can be used skillfully after you are the first step, if you put back that article can also be thoroughly understand, is to be considered to move towards the middle of the stage.

  



Detailed broadcast mechanism, broadcast receiver,android advanced Prerequisite Knowledge (I.).

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.