Android program development - broadcast application, master Android, master the application of broadcast BroadcastReceiver, use static creation method to create a MyBroadcastReceiver broadcast receiver, handle the OnReceiver method

1. Broadcast application

1.1 Experimental purpose

  Master Android Master the application of broadcast BroadcastReceiver

1.2 Experimental steps

1. Create an Android application and practice the method of statically creating broadcasts.

2. Use static creation to create a MyBroadcastReceiver broadcast receiver and handle the OnReceiver method

3. Start another application to practice the method of dynamically creating broadcasts

4. Use the dynamic creation method to create a broadcast with code in MainActivity, monitor the sending of text messages, and obtain the content of text messages.

1.3 实验代码

1. Create a project

2.Create a MyBroadcastReceiver broadcast receiver using static creation and handle the OnReceiver method:

Configure broadcast receivers in the AndroidManifest.xml manifest file

Note that the package name must be changed to your own package name

3. Add two controls to the activity_main layout file

Experimental effect

4. Add control initialization and click event processing to MainActivity, and send a broadcast

At this point, the static creation of broadcast has been completed. Deploy it to see the effect.

Let's create it dynamically and start another application.

5. Add the following initialization code to MainActivity to monitor the content of text messages.

6. Add the permission to receive text messages in the AndroidManifest.xml manifest file

7. At this point, the dynamic creation of the broadcast application has been completed. Deploy it to see the effect, and then send a text message to the virtual machine to see if Logcat prints the content of the text message.

1.4 Experimental Summary

1.BroadcastReceiver

BroadcastReceiver is a means of communication. The system itself will send broadcasts many times, such as when the battery is low or sufficient, just after startup, plugging in headphones, input method changes, etc., these occur Event, the system will send a broadcast, called system broadcast, and each APP will receive it. If you want your application to do some operations when receiving this broadcast, such as secretly running services in the background after the system is turned on, you only need to register a service for your application at this time. Monitor the powered-on BroadcastReceiver. We can also send broadcasts ourselves. For example, after receiving push information from the server and the user logs in elsewhere, the user should be forced to log off and return to the login interface, and be prompted to log in elsewhere .

1.4.1 There is a problem

Guess you like

Origin blog.csdn.net/WZY22502701/article/details/134223576