Elementary Android Software Development Tutorial: How to Send and Receive Broadcast

Elementary Android Software Development Tutorial: How to Send and Receive Broadcast

In the process of Android software development/Android mobile phone application development, if you need to communicate between two completely unrelated programs, you can use the mechanism of sending broadcast and receiving broadcast. For example, program A sends a broadcast program B accepts to do something In this way, mutual communication is achieved.

1. Broadcast receiver

The broadcast receiver is simply the Java class that receives the broadcast intent. This Java class inherits the BroadcastReceiver class and rewrites:

public void onReceive(Context context, Intent intent), where the intent can obtain the passed data;

Broadcast intent is the intent sent through Context.sendBroadcast(Intent intent) or Context.sendOrderedBroadcast(Intent intent). Through this statement, it can be broadcast to all components that meet the conditions. For example, if the intent is set to action=”com.xiazdong”, then all All broadcast receivers set in AndroidManifest.xml can receive the broadcast.

Note: If the service is not up, we will not be able to accept the broadcast, so we must ensure that the time when the service is accepted is open. The service in the above example is open when opening the activity, but if the user turns off the handle and then turns it on, so If the service does not open the status, it is very risky. At this time, the scrvice will not accept any news unless the user enters the activity again, and will help him open the scrvice.

2. Broadcast sender

The general broadcast sender is the program that calls Context.sendBroadcast(), and the broadcast receiver is the program that inherits BroadcastReceiver;

Generally, broadcast senders go through implicit intentions so that they can be sent to multiple people; broadcast senders are divided into synchronous broadcast and ordered broadcast:

a. Synchronous broadcast: After the sender announces it, it almost arrives at multiple broadcast receivers at the same time. After a certain receiver cannot receive the broadcast, it will be sent to the next receiver after some processing, and the broadcast cannot be stopped for continuous transmission; Context. sendBroadcast(intent);

b. Orderly broadcast: The broadcast receiver needs to set the priority in advance, the one with the higher priority receives the broadcast first, and the priority value is -1000~1000, set in AndroidManifest.xml; for example, there are 3 broadcast receivers A and B , C, priority A>B>C, so A receives the broadcast earliest, when A receives the broadcast, it can add some data to the broadcast to the next receiver (intent.putExtra()), or stop the broadcast (abortBroadcast ());Context.sendOrderedBroadcast(intent).

Well, I have said so many basic Android software development tutorials on how to send and receive broadcasts. If you have this skill, or you have nowhere to use it, or you can’t find talents, then the editor will recommend a platform for you, Yipinwei Customer, APP development exclusive customization. One-stop service and APP developers are all here. You can hire a lot of talents on the platform. Of course, if you have the skills, you can also take orders on it. Yipinweike.com was founded in 2010 and is a well-known knowledge and skill sharing service in China. The platform has more than 21 million registered users. The platform gathers tens of millions of service providers including design, development, planning, marketing and other categories. The platform breaks the imbalanced distribution of China's creative service talents and corporate needs, builds an innovative trading model and a complete service system, and successfully serves more than one hundred Ten thousand enterprises, institutions and individual employers. The types of services (tasks) provided by the platform cover more than 300 detailed items in seven categories: design, development, decoration, copywriting, marketing, business, and VR.

Sometimes a project may require more than 10 years of developer experience. This is difficult to find, but with the help of Yipinweike.com platform, it is easy to match it and can help me find it in a few hours. Talents. "Compared to other platforms, Yipin Weike.com has a faster response time, and the recommended service providers are better.

Guess you like

Origin blog.csdn.net/epwk01/article/details/108242126