当前位置:网站首页>Differences between broadcastreceiver and localbroadcastmanager

Differences between broadcastreceiver and localbroadcastmanager

2022-06-23 23:42:00 MirkoWug

1. Program A Can I receive the program B Broadcast of ?
can , Use global BroadCastRecevier Can carry on the cross process communication , But notice that it can only passively receive radio . Besides ,LocalBroadCastRecevier Limited to inter broadcast communication of this process .

2. Is there a limit to the amount of data broadcast can transmit , How much is the , Why limit ?
Intent There is a size limit when passing data , Approximately limited to 1MB within , Do you use Intent To transfer data , In fact, it is cross process communication (IPC), Cross process communication requires data from the kernel copy Into the process , Each process has a buffer to receive kernel data , The default is 1M; If the data transferred at one time exceeds the limit , There will be an exception .

The performance of different manufacturers is different. It may be that the manufacturer has modified the size of this limit , It is also possible that the same object has different sizes on different machines .

Delivering big data , Should not use Intent; Consider using ContentProvider Or share memory anonymously . In simple cases, segmented transmission can be considered .

3. There are several kinds of radio registration , What are the advantages and disadvantages of each ?
The first is the resident type ( Static registration ): When the application closes, if there is a message broadcast , The program will also be called by the system , Self run .

The second is not permanent ( Dynamic registration ): The broadcast follows the life cycle of the program .

Dynamic registration

advantage : stay android In the broadcast mechanism of , Dynamic registration priority is higher than static registration priority , So if necessary , It is necessary to dynamically register broadcast recipients .

shortcoming : When used to register Activity Turn it off , And then the radio is dead .

Static registration

advantage : There is no need to worry about whether the broadcast receiver is turned off , As long as the device is on , The radio receiver is on .

4. How to intercept and abort A text message ?
Can monitor this signal , When passed to the real receiver , We will customize the broadcast receiving program to have priority over it , And cancel the broadcast , In this way, the function of intercepting short messages can be realized .

5. BroadcastReceiver,LocalBroadcastReceiver difference ?
1、 Application scenarios
1、BroadcastReceiver It is used to transfer messages between applications ;

2、 and LocalBroadcastManager Used to deliver messages within an application , Than broadcastReceiver More efficient .

2、 Security
1、BroadcastReceiver The use of Content API, So essentially it's cross application , So when you use it, you must consider not to be abused by other applications ;

2、LocalBroadcastManager There's no need to think about security , Because it only works inside the application .

3、 In principle
(1) And BroadcastReceiver In order to Binder The communication mode is different from the underlying mechanism ,LocalBroadcastManager The core implementation of is actually Handler, It's just using IntentFilter Of match function , as for BroadcastReceiver It doesn't matter if you switch to another interface , By the way, I've made use of existing classes and concepts .

(2) LocalBroadcastManager the reason being that Handler Communication within the application , Better natural safety , More efficient .

  1. How to make the program start automatically ?
    Define a Braodcastreceiver,action by BOOT——COMPLETE, Start the program after receiving the broadcast .
原网站

版权声明
本文为[MirkoWug]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206232008325051.html