How to set permissions in broadcast sender and receiver in android

后端 未结 5 1267
轮回少年
轮回少年 2020-12-04 09:46

How do we specify in broadcast sending application that which application can receive this broadcast, and in receiving application that which particular application has the

5条回答
  •  渐次进展
    2020-12-04 10:27

    Declare permission

    First you need declare your permission in your AndroidManifest.xml

    
    
    

    the android:name value is used as permission value and will used later.

    Usage

    There are two kinds of permission usages related to broadcast receiver:

    (1) Control which application can receive your broadcast:

    String PERMISSION_STRING_PRIVATE_RECEIVER = "YOU_NEED_THIS_TO_RECEIVE_THIS_BROADCAST"
    sendBroadcast(intent, PERMISSION_STRING_PRIVATE_RECEIVER);
    

    With this usage, you can control only authorized application can handle the broadcast you sent.

    (2) Only handle the broadcasts have the specified permission

    String PERMISSION_STRING_PRIVATE_BROADCASTER = "ONLY HANDLE BROADCASTS WITH THIS PERMISSION"
    IntentFilter filter = new IntentFilter(ACTION_SAMPLE);
    registerReceiver(mReceiver, filter, PERMISSION_STRING_PRIVATE_BROADCASTER, null);
    

    With this usage, you can make sure that the broadcaster is authorized.

提交回复
热议问题