Fifth chapter Global Horn, detailed broadcasting mechanism

Source: Internet
Author: User

What we know is that the maximum IP address of an IP address segment in network communication is the broadcast address. For example, if the IP address of a network is 192.168.0.XXX, and his subnet mask is 255.255.255.0, then the IP address is 192.168.0.255.

Android also has a corresponding broadcast mechanism, this chapter will detail the details.

1. Brief introduction of broadcasting mechanism

    • The broadcasting mechanism of Android consists of broadcast, broadcast receiver (broadcast receiver) and broadcast content;
    • Each application can register its own broadcasts of interest, which means that the program will only accept its own "interested" broadcast content;
    • Android provides a complete set of APIs that allow programs to freely send and receive broadcasts.
    • Send the broadcast we mentioned before, Intent, while receiving the broadcast is done by the broadcast receiver (broadcast receiver)

Type of broadcast:

    • Standard broadcast: (normal broadcasts) a fully asynchronous broadcast that is received by almost all broadcast receivers at the same time after a broadcast is issued. This means that the broadcast is more efficient, but that he cannot be truncated and controlled. The work flow is as follows

????

    • Ordered broadcast: (Ordered braoadcasts) a broadcast that is performed synchronously. After the broadcast is sent, only one broadcast can accept the broadcast content at the same time, and when the broadcast finishes its own logic, it may be passed to the following broadcast.
      • The above means that the broadcast receiver is sequential, that is, the broadcast receiver is a priority concept
      • Broadcast receivers can intercept broadcast messages
      • Work Flow:

      ?

2. Receiving system broadcasts

What needs to be understood is that the Android system has built in a lot of system broadcasts, they are constantly relaxing broadcast messages. For example, a mobile phone to send a broadcast, mobile phone low power to send a broadcast.

So how do you use a broadcast receiver to receive system broadcasts?

2.1 Dynamic Registration monitoring network changes

???? There are generally two ways of registering a broadcast, registering it in code and registering it in Androidmanifest.xml, where the former is also known as dynamic registration, which is also known as static registration.

???? So how do you create a broadcast receiver? You just need to create a new class, let it inherit from Broadcastreceiver, and rewrite the parent class's OnReceive () method. So when a broadcast arrives, the OnReceive () method is executed, and the specific logic can be handled in this method.

?

This code is simple to create a dynamic registration of the broadcast process, we need to optimize it-- but just to remind the network has changed is not enough human, it is best to tell the user exactly whether there is a network or no network, so we also need to further optimize the above code

???? In the process of rewriting the above deserves special attention, the Android system in order to ensure the security of the application, if the program needs to access some of the critical information of the system, you must declare the permissions in the configuration file, or the program will crash directly, For example, the network status of the query system is required to declare permissions.

Declared in the Androidmanifest.xml file: <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>

2.2 Static registration for boot start

???? dynamically registered broadcast receivers have the freedom to control registration and logoff and have a great advantage in terms of flexibility, but there is also a drawback that the broadcast must be received after the program is started, because the logic of the registration is written in OnCreate () Method in the. So is there any way that the program can receive the broadcast without booting? This requires the use of a static registration method.

The static Registration method places the registered logical action in the Androidmainfest.xml file. Of course, because the registered behavior is placed in Androidmainfest.xml during static registration, the class inheriting broadcastreceiver cannot be defined as an inner class and needs to be defined as a separate class

S1: Create an entity class that inherits Broadcastreceiver and override the OnReceive () method

S2: Registering in the Androidmainfest.xml file and declaring permissions

A new label <receiver> appears in the <application> tab, and all statically registered broadcast receivers are registered here. Its usage is very similar to the <activity> tag, first by Android:name to specify which broadcast receiver to register, and then in the <intent-filter> tag to add the broadcast you want to receive it. , since the Android system launches a broadcast with a value of Android.intent.action.BOOT_COMPLETED, we have added the corresponding action here.

In addition, the monitoring system to start the broadcast is also required to declare permissions, you can see, we use the <uses-permission> tag and added a Android.permission.RECEIVE_BOOT_COMPLETED permission.

3. Send Custom Broadcasts

Custom broadcasts are non-system-sent broadcasts, and to be honest, just write a broadcast receiver and register it. There is no way to define how a broadcast class has broadcast content to be emitted.

There are, of course, two types of custom broadcasts-standard broadcasts and ordered broadcasts.

3.1 Sending standard broadcasts

S1: Create a new class that inherits the Broadcastreceiver class ———— Mybroadcastreceiver.java, and overrides the Onreceiver () method;

S2: Registration and empowerment of the above broadcast receivers in Androidmainfest.xml;

S3: Adds a button to a layout that triggers the Send broadcast function. Here we choose the Activity_main.xml file;

S4: The ability to send broadcasts in the Listener method of the button:

???????? Intent Intent = new Intent ("Com.exmple.broadcasttest.MY_BROADCAST");

???????? Sendbroadcast (Intent);

?

3.2 Sending an ordered broadcast

???? The difference between ordered and unordered broadcasts:

???????? This is done when you send the broadcast:

???????????? Intent Intent = new Intent ("Com.exmple.broadcasttest.MY_BROADCAST");

???????? Sendbroadcast (Intent,null);

So how do you set the order of the broadcast receivers?----priority, defined in the Androidmainfest.xml file

So how do you truncate this custom broadcast?---call the Abortbroadcast () method in the Onreceiver () method of Mybroadcastreceiver, which indicates truncation

4. Use local broadcast

Android introduces a set of local broadcast mechanisms that use this mechanism to send broadcasts only within the application, and broadcast receivers can only receive broadcasts from the application.

The local broadcast mechanism enables applications to share and deliver information using broadcasts, which is only visible within the program and guarantees security.

Finally, let's take a look at some of the advantages of using local broadcast.

1. It is clear to know that the broadcast being sent will not leave our program, so there is no need to worry about confidential data leaks.

2. Other programs cannot send broadcasts to the inside of our program, so there is no need to worry about security breaches.

3. Sending a local broadcast is more efficient than sending a system global broadcast.

?

5, the best implementation of the broadcast-the implementation of forced downline function

Combined with the content described above to implement an app login interface has a button, click on the auto-downline back to the login screen. Here Auto jump using broadcast implementation!

6. Git time, first knowledge version control tool

6.1 Installing Git

1,google Git Windows7 Download

download:https://git-scm.com/

2, installation

3, configuration

???????? git config--global user.name "metitation"

???????? git config--global user.email "[email protected]"

6.2 Creating a code Warehouse

Creating a code warehouse is simple, just executing the command git init

????

This is done after you have completed multiple folders. git/, If you want to delete the local repository, just delete this folder .

6.3 Submit Local Code

?

7. Summary and comments

In conclusion, I will not copy the book, there is an extension task------"How to build a separate blog--Concise Github pages and Jekyll tutorial"

http://cnfeat.com/blog/2014/05/10/how-to-build-a-blog/

Try it when you're free

Fifth chapter Global Horn, detailed broadcasting mechanism

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.