Android uses Localbroadcastmanager to solve broadcastreceiver security issues

Source: Internet
Author: User

In Android, Broadcastreceiver is designed to be considered globally to facilitate communication between applications and systems, applications, and applications, so broadcastreceiver is a security issue for a single application. The corresponding problems and solutions are as follows:

1. When the application sends a broadcast, the system will match the sent intent with the intentfilter of all registered Broadcastreceiver in the system and execute the corresponding onreceive function if the match succeeds. An interface similar to Sendbroadcast (Intent, String) can be used to specify the permission that the receiver must have when sending broadcasts. or set a broadcast through Intent.setpackage is only valid for a program.

2. When an application registers for a broadcast, even if the intentfilter is set up, it will receive a broadcast from another application to match the decision. Broadcasts for dynamic registration can be via similar registerreceiver (Broadcastreceiver, Intentfilter, String, Android.os.Handler) interface specifies the permission that the sender must have, and for statically registered broadcasts, the android:exported= "false" property indicates that the recipient is not available to the external application, that is, the broadcast from outside is not accepted.

The above two problems can be solved by Localbroadcastmanager:

The Android V4 Compatibility Pack provides Android.support.v4.content.LocalBroadcastManager tool classes to help you send and register local broadcasts within your own process, using it more than directly through Sendbroadcast (Intent) Sending system global broadcasts has the following advantages.

1 because broadcast data spreads within the scope of this application, you do not have to worry about the disclosure of privacy data.

2 Do not worry about other applications to forge broadcasts, causing security risks.

3 It is more efficient than sending a global broadcast within the system.

Above reference: http://blog.csdn.net/t12x3456/article/details/9256609

To register a broadcast:

public class mainactivity Extents activity{     intentfilter filter;     Localbroadcastmanager Mlocalbroadcastmanager;     @Override    protected void onCreate (Bundle savedinstancestate) {            ...        Filter = new Intentfilter ();        Filter.addaction (action);        Mlocalbroadcastmanager = Localbroadcastmanager.getinstance (this);//Get Instance        Mlocalbroadcastmanager.registerreceiver (receiver, filter);//register for monitoring}  private Broadcastreceiver receiver = new Broadcastreceiver () {        @Override public        void OnReceive (context context, Intent Intent) {            if ( Intent.getaction (). Equals (action)) {               //do someting            }        }    ;  @Override    protected void OnDestroy () {        Super.ondestroy ();        Mlocalbroadcastmanager.unregisterreceiver (receiver);//Cancel Monitoring, note unregisterreceiver () method from Localbroadcastmanager;    }}

Send broadcast:

public class Secondactivity extends activity{       localbroadcastmanager LBM;       @Override        protected void onCreate (Bundle savedinstancestate) {             ...            LBM = Localbroadcastmanager.getinstance (this);           Findviewbyid (R.id.button). Setonclicklistener (New View.onclicklistener () {            @Override public            void OnClick ( View v) {                 Intent Intent = new Intent (mainactivity.action);                 Lbm.sendbroadcast (intent);}}        );}}

  

Android uses Localbroadcastmanager to solve broadcastreceiver security issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.