Android请求权限不适用于通知权限

时间:2016-10-16 10:22:38

标签: android android-service android-permissions android-service-binding

为什么此代码有效:

ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            666);

## In the manifest
<uses-permission android:name="android.permission.READ_CONTACTS" />

并且此代码无效:

ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE},
            666);

## In the manifest
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />

第一个代码段启动请求权限活动,询问读取联系人权限。 最后一个代码段不显示活动,在日志中有以下消息:

10-16 12:19:48.061 1138-3635/? I/ActivityManager: START u0 {act=android.content.pm.action.REQUEST_PERMISSIONS pkg=com.google.android.packageinstaller cmp=com.google.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity (has extras)} from uid 10147 on display 0

那么,我应该做什么来询问通知权限? 目标API是v24,最小sdk版本是23。

3 个答案:

答案 0 :(得分:2)

嗯,运行时权限仅被要求提供危险权限,因为通知服务不是您不需要的危险权限。这是所有危险和非危险权限的列表。

https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous

P.S。无论许可是危险还是正常,都必须在舱单中声明,唯一的区别在于运行时请求。

答案 1 :(得分:1)

来自documentation

BIND_NOTIFICATION_LISTENER_SERVICE

String BIND_NOTIFICATION_LISTENER_SERVICE

Must be required by an NotificationListenerService, to ensure that only the system can bind to it.

这意味着它不一定需要在运行时由用户授予。相反,它需要在NotificationListenerService下的manifest.xml中声明。 像这样的东西,

<service android:name=".NotificationListener"
          android:label="@string/service_name"
          android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
     <intent-filter>
         <action android:name="android.service.notification.NotificationListenerService" />
     </intent-filter>
 </service>

答案 2 :(得分:0)

在这里,您可以看到您需要提出的权限列表 https://developer.android.com/guide/topics/security/permissions.html

  

READ_CALENDAR,WRITE_CALENDAR

     

CAMERA

     

READ_CONTACTS,WRITE_CONTACTS,GET_ACCOUNTS

     

ACCESS_FINE_LOCATION,ACCESS_COARSE_LOCATION,RECORD_AUDIO

     

READ_PHONE_STATE,CALL_PHONE,READ_CALL_LOG,WRITE_CALL_LOG,ADD_VOICEMAIL   USE_SIP,PROCESS_OUTGOING_CALLS

     

BODY_SENSORS

     

SEND_SMS,RECEIVE_SMS,READ_SMS,RECEIVE_WAP_PUSH,RECEIVE_MMS

     

READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE