Android broadcastreceiver receives the broadcast of SMS

1、 Knowledge introduction

1. Broadcastreceiver is one of the four components of broadcast receiver.

2. Android has built-in many system level broadcasts, which can get the status information of various systems in the application.

3. Usage scenario:

① when the mobile phone has no power, it will inform the user of the broadcast message of "low power". The app in use can receive this broadcast message. If it is a game, it may remind you to save the data. If it is a working software in use, it may remind you to connect the power supply.

[prompt] for a certain program, you can choose to receive this message or not.

② if the storage of the mobile phone is insufficient, you will be prompted to clean up the space, otherwise the application will slow down or cannot be used.

③ as soon as the mobile phone is turned on, so and so program will run automatically, and QQ and wechat will start automatically.

4. One program sends broadcast information and one program receives broadcast information, so at least two programs are required to complete one sending and one receiving. The sender is called the broadcast sender; The receiver is called the broadcast receiver.

5. Broadcast recipients need to register. Dynamic registration, static registration (in androidmanifest file)

2、 Project practice

[step] ① define broadcast receiver

② register and add an intent filter to receive specific broadcasts

③ rewrite the onreceiver method. If it is executed, it means that the broadcast has been received

④ define the receiver of received information broadcast. If it belongs to system broadcast, you need to apply for permission (static and dynamic)

[project structure]

[prompt] to create a receiver, you can use the following methods. This method will automatically register the receiver in the androidmanifest, but you still need to add an intent filter to receive a fixed broadcast

【AndroidManifest】  

【MsgReceiver】

Override the onreceiver method and add toast

[add permission]

① Static permission: [androidmanifest]

② Dynamic permission [mainactivity]

[test]

① Operation

② Effect

[summary]

① here is the broadcast of the receiving system, or you can define the broadcast yourself. And many functions can be added to receive the broadcast for specific operations.

② a filter must be added to the broadcast to receive a specific broadcast

③ if you want to apply for permission for receiving system broadcast, you must apply for dynamic permission after 5.0

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>