How to set permissions in broadcast sender and receiver in android

后端 未结 5 1272
轮回少年
轮回少年 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:35

    To control who is able to receive the broadcast message, you can use the method sendBroadcast:

    public abstract void sendBroadcast (Intent intent, String receiverPermission)
    

    where you precise the name of the required permission. If the receiver does not declare this permission, it will not be able to get the message. For example, the broadcast sender can do:

    Intent broadcast = new Intent(this, MyBroadcastReceiver.class);
    sendBroadcast(broadcast, "andro.jf.mypermission");
    

    In the manifest of the broadcast sender, a new permission should be declared:

    
    
    

    Then, in the application that is supposed to receive this broadcast, you have to declare this permission and say that you use it. In the manifest you can add:

    
    
    

    and of course, you have to declare your broadcast receiver:

    
    

    You can have a look at this post for a complete example of a custom permission and also the android developer page about this. Be carefull with the order of installation of your apps because the one that defines the permission should be installed first.

提交回复
热议问题