"Case study" Android broadcast does not receive a cause analysis

Source: Internet
Author: User

Overview

I am learning Android development, and recently, when practicing Android custom broadcasts, I found out how the broadcast was not received. It is strange to see many students on the Internet have encountered the same problem. A special summary today for your reference. Of course, if there is a mistake, I hope you correct me.

This discussion is the permission of the broadcast access problem, for the display or implicit problems, not here to repeat .

Version

API version is V21

"Overview of the Problem"

Let me introduce some examples of my study. It is very simple to trigger a custom broadcast via a button.

The code is as follows.

1  Public classMybroadcastreceiverextendsBroadcastreceiver {2 3 @Override4      Public voidOnReceive (Context context, Intent Intent) {5LOG.I ("TEST", "received in Mybroadcastreceiver");6         7Toast.maketext (Context, "received in Mybroadcastreceiver", Toast.length_long). Show ();8     }9 Ten}

The interface section is a button, which is a little bit.

The button logic is as follows:

receive permissions are set because of security issues. The second parameter of Sendbroadcast is, Com.example.broadcasttest.RECV_MYBC. (This is also the cause of this problem)

PS: If you do not set access rights, there will be exported receiver does not require permission alarm. The reason is that a third-party app can trigger the logic by sending the broadcast, which creates a security risk. Specific details can be Baidu a bit.

Com.example.broadcasttest.MY_BROADCAST is a custom broadcast.

 Public classMainactivityextendsActivity {PrivateButton Buttonsend; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Buttonsend=(Button) Findviewbyid (r.id.button_send); Buttonsend.setonclicklistener (NewOnclicklistener () {//            //trigger a custom broadcast, broadcast defined as Com.example.broadcasttest.MY_BROADCAST//set the Receive permission to RECV_MYBC//@OverridePublic   void  OnClick (View v) {Intent Intent = new Intent ("Com.examp Le.broadcasttest.MY_BROADCAST "); Sendbroadcast (Intent,"Com.example.broadcasttest.RECV_MYBC");    }        }); }}

Androidmanifast.xml, you can also add permissions to this receiver.

<receiverAndroid:name= "Com.example.broadcasttest.MyBroadcastReceiver"android:permission= "Com.example.broadcasttest.RECV_MYBC" >            <Intent-filter>                <ActionAndroid:name= "Com.example.broadcasttest.MY_BROADCAST" />            </Intent-filter>        </receiver>

The phenomenon is that the broadcast accepts failure.

"Problem root cause"

The permissions here are not stated, and any custom permissions need to be defined in Androidmanifast.xml.

"Solutions"

To add a permission statement and permission usage to a custom permission in Androidmanifest.xml

    < Permission         Android:name = "Com.example.broadcasttest.RECV_MYBC"         android:protectionlevel= "normal"/>    <  android:name= "Com.example.broadcasttest.RECV_MYBC"/>

Note the points:

1. It is successful to not set access permissions. Permissions are not required.

2. The name of the custom permission to bring the package name, without the package name may be problematic.

Practice code Download:

http://download.csdn.net/detail/mimiduck/9430272

"Case study" Android broadcast does not receive a cause analysis

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.