Android uses LocalBroadcastManager to solve BroadcastReceiver security problems and broadcastreceiver

Source: Internet
Author: User

Android uses LocalBroadcastManager to solve BroadcastReceiver security problems and broadcastreceiver

In the Android system, BroadcastReceiver is designed globally to facilitate communications between applications and systems, applications, and applications, therefore, for a single application, BroadcastReceiver has security problems. The corresponding problems and solutions are as follows:

1. When an application sends a broadcast, the system matches the Intent sent with the IntentFilter of all registered BroadcastReceiver in the system. If the match succeeds, the corresponding onReceive function is executed. You can use an interface similar to sendBroadcast (Intent, String) to specify the permission required by the receiver when sending a broadcast. Or you can use Intent. setPackage to set the broadcast to only be valid for a program.

2. When an application registers a broadcast, even if IntentFilter is set, it still receives the broadcast from other applications for matching and judgment. For Dynamic Registration broadcast, you can use a broadcast like registerReceiver (BroadcastReceiver, IntentFilter, String, android. OS. handler) interface specifies the permission that the sender must possess. For static registration broadcasts, you can use the android: exported = "false" attribute to indicate that the receiver is unavailable to external applications, that is, broadcast from outside is not accepted.

The above two problems can be solved through LocalBroadcastManager:

Android v4 compatibility package provides android. support. v4.content. the LocalBroadcastManager tool class helps you to send and register local broadcasts in your own processes. Using it has the following advantages over sending system global broadcasts through sendBroadcast (Intent.

1. Because broadcast data is transmitted in this application, you do not have to worry about the leakage of private data.

2. Do not worry about other applications forging broadcasts, which may cause security risks.

3 It is more efficient than sending global broadcasts in the system.

Reference: http://blog.csdn.net/t12x3456/article/details/9256609

Register 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); // obtain the instance mLocalBroadcastManager. registerReceiver (receiver, filter); // register listener} private BroadcastReceiver receiver ER = 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 listening. Note that the unregisterReceiver () method is 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);            }        });}}

  

Related Article

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.