As we all know, the four main components of Android are Activity, Service, ContentProvider and Broadcast. We can see the importance of Broadcast. What is a broadcast mechanism? I think we are no stranger to the broadcast. The broadcast we send has no purpose, and there is no clear goal. As for who will receive it, the person who sent the broadcast will certainly not know.
The same is true for broadcast in android. An activity sends a broadcast and does not know which activity will respond. So what kind of activity will respond? How does an activity send broadcasts? What should we do with broadcast? Next, let's take a step.
First, how does an activity send a broadcast?
Is through the sendBroadcast function. The function parameter is an intent object. The most important thing about this object is to define the action. After the broadcast is sent, other activities that can process this action can receive broadcasts and respond.
So how do other activities know what actions they should accept?
Is through the filter fliter. All actions that can be responded are defined in the filter. If the action sent by the broadcast exists in the filter, the activity responds, and vice versa, the broadcast is ignored.
After receiving the broadcast, what operations should the activity perform?
Here, we will talk about the broadcast response through the BroadcastReceiver class. We inherit the class and reload its onReceive method to customize the processing after execution.
We also need to know what action is.
The so-called action, that is, action, is generally a description of the operation to be performed. Android allows us to customize many actions, such as ACTION_EDIT and ACTION_VIEW. They describe the operations to be performed, just like ACTION_EDIT, and describe the operations as editing. Then the activity that can implement the editing function will respond. There are many system-defined actions, which will not be described here. For details, refer to the official documentation. In addition to the official offer, we use more custom actions and describe the actions to be implemented, the general expression is "package name" + ". "+" Action Description ". For example, "com. suns. HTTPRequestActivity. BUTTONCHANGE" is decomposed into "com. suns. HTTPRequestActivity" + "." + "BUTTONCHANGE ". You can customize the action of a button change. In fact, this is just a naming rule string, in order to allow intent and fliter to correspond, and let people know what this action is.
Can broadcast be implemented in several ways? What are their differences?
There are two broadcast methods. One is in androidmanifest. the other method is to dynamically register broadcasts in the Code. Both methods can implement the broadcast mechanism, but they are still quite different, therefore, the application should be divided into situations. If we are in androidmanifest. if it is defined in xml, the broadcast will not end after the activity ends, because it has been written in manifest. in the xml file, that is, it is registered to the system. Therefore, whether or not your activity exists has no impact on the broadcast. The Dynamic Registration of broadcast in java code can be canceled after the activity ends, that is, it disappears as the activity disappears. This solution should be clear to everyone. For some system applications, such as mobile phones that have no power, and background computing traffic, we can use androidmanifest. register in xml, and some broadcasts that make sense only when the activity exists, such as modifying the interface, are suitable for Dynamic Registration, and no activity is available. What is the purpose of this broadcast? It will only waste resources.
Well, after talking about this, we still need to use an example to apply it.
First, register the broadcast dynamically, click the button on the interface, send the broadcast that changes the button, and then the button changes to the progress bar.
The Code is as follows:
Broadcasttest. java:
- package com.suns.ButtonChanged;
-
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.view.View;
-
- public class Broadcasttest extends BroadcastReceiver{
-
- @Override
- public void onReceive(Context arg0, Intent arg1) {
- // TODO Auto-generated method stub
- ButtonChangedActivity.getButton().setVisibility(View.GONE);
- ButtonChangedActivity.getBar().setVisibility(View.VISIBLE );
-
- }
- }
NextButtonChangeActivity. java implementation codeAndMain. xml