Android BroadcastReceiver basic description 1
-Overview of BroadcastReceivcer
1. What is broadcast BroadcastReceiver? It is one of the four major Android components. In essence, BroadcastReceiver is a global listener used to listen to broadcast messages globally in the system. Therefore, it can easily implement communication between different components.
2. Create and start BroadcastReceiver
BroadcastReceiver is the Broadcast Intent used to receive the Broadcast from the program. It is the same as the Activity and Service started by the application. You only need two steps:
① Create the Intent of the Broadcast to be started
② Create a class that inherits BroadcastReceiver, register the Receiver in the list file, and call the SendBroadcast () or sendOrderedBroadcast () (send ordered broadcast) method of content to start the specified BroadcastReceiver.
Note: When an application sends a Broadcast Intent, all BroadcastReceiver matching the Intent will start. If you do not need to send broadcast across applications, consider using this typeLocalBroadcastManager
12345678910111213 |
protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_startbroadcast = (Button) this .findViewById(R.id.bt_startbroadcast); bt_startbroadcast.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MainActivity. this , myBroadcast. class ); intent.putExtra( "broadcast" , "hello,world! broadcast" ); sendBroadcast(intent); } }); } |
123 |
public void onReceive(Context context, Intent intent) { Toast.makeText(context, "receiver-----" + intent.getStringExtra( "broadcast" ), 1 ).show(); } |
3. Ordered Broadcast
Normal playback(SendContext. sendBroadcast) Is completely asynchronous. All BroadcastReceiver is run in an undefined order, often at the same time. This is more efficient, but it means that the BroadcastReceiver cannot be used or aborted.
Ordered Broadcast(SendContext. sendOrderedBroadcast) Delivered to a BroadcastReceiver. Because each BroadcastReceiver returns a value after execution, it can be propagated to the next BroadcastReceiver, or it can terminate the broadcast completely through the abortBroadcast () method, so that it will no longer pass through the BroadcastReceiver. In the configuration file, you can control that the attribute running android: priority = "" matches intent-filter. The same BroadcastReceiver priority will run in any order.
12345678910111213141516 |
public class MainActivity extends Activity { private Button bt_start; protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_start = (Button) this .findViewById(R.id.bt_start); bt_start.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.setAction( "abc" ); intent.putExtra( "name" , "hello,BroadcastReceivcer" ); sendBroadcast(intent); } }); } |
1234 |
public void onReceive(Context arg0, Intent intent) { String name = intent.getStringExtra( "name" ); System.out.println( "one=======" + name); } |
12345 |
< receiver android:name = "com.example.broadcast_order.One" > < intent-filter android:priority = "10" > < action android:name = "abc" /> intent-filter > receiver > |
12345678 |
public class Two extends BroadcastReceiver { public void onReceive(Context arg0, Intent intent) { String name = intent.getStringExtra( "name" ); System.out.println( "Two=======" + name); // Cancel the Broadcast Transfer abortBroadcast(); } } |
12345 |
< receiver android:name = "com.example.broadcast_order.Two" > < intent-filter android:priority = "12" > < action android:name = "abc" /> intent-filter > receiver > |
Ii. lifecycle of the workerBecause BroadcastReceiver is essentially a listener, the method for implementing BroadcastReceiver is also very simple. You only need to override the onReceiv (Content content, Intent intent) method in the BroadcastReceiver method.
Each time a system Broadcast event occurs, the system creates an instance of the corresponding BroadcastReceiver and automatically triggers its onReceive () method. After the onReceive () method is executed, the Broadcast instance is destroyed. That is to say, the life cycle of Broadcast is the onReceive () method.
If the Broadcast onReceive () method cannot be executed within 10 seconds, Android considers the program to be unresponsive. Therefore, do not perform time-consuming operations in the BroadcastReceiver onReceive () method. Otherwise, the ANR dialog box is displayed. To perform time-consuming operations, consider starting a Service of Intent to complete the operation. You should not consider using a new thread to complete time-consuming operations, because the life cycle of BroadcastReceiver is too short.
Iii. Battery monitoring by using the battery Controller
12345678910111213 |
protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_startbroadcast = (Button) this .findViewById(R.id.bt_startbroadcast); bt_startbroadcast.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { // Two methods can be used to specify Broadcast to match Intent. One is to write configurations in the configuration file, and the other is to specify the following methods in the Code: IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); myBroadcast receiver = new myBroadcast(); // New class for customizing BroadcastReceiver registerReceiver(receiver,filter); } }); } |
12345678 |
public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(intent.ACTION_BATTERY_CHANGED)) { // Obtain the current power int level = intent.getIntExtra( "level" , 0 ); // Obtain the total power int scale = intent.getIntExtra( "scale" , 100 ); Toast.makeText(context, "Battery :" + ((level * 100 ) / scale) + "%" , 1 ).show(); } |
Note: add the permission to obtain the power status in the list file:
4. Automatic startupStart activities or Service in the Custom BroadcastReceiver
12345 |
public void onReceive(Context context, Intent intent) { Intent intent1 = new Intent(context, BootActivity. class ); Intent1.setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); // use setFlag () when registering an Activity (). Service not required context.startActivity(intent1); } |
Register aggreger in the configuration file
123456 |
< receiver android:name = "com.example.BroadcastReceiverDemo.myBroadcast" > < intent-filter > < action android:name = "android.intent.action.BOOT_COMPLETED" /> < category android:name = "android.intent.category.HOME" /> intent-filter > receiver > |
Add the permission to access the system at startup: