BroadcastReceiver 中的权限错误

Permission error in BroadcastReceiver

我在清单文件中授予了 READ_SMSREAD_CALL_LOG 权限,但我的 BroadcastReceiver.

中仍然得到 SecurityException

代码:

@Override
public void onReceive(Context context, Intent intent) {
    getMissedCallCount(context);
    getUnreadSMSCount(context);
}

private void getMissedCallCount(Context context) {
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
        Log.d("Call Logs permission", "Not provided...");
        return;
    }
    String[] projection = {CallLog.Calls.CACHED_NAME, CallLog.Calls.CACHED_NUMBER_LABEL, CallLog.Calls.TYPE};
    String where = CallLog.Calls.TYPE + "=" + CallLog.Calls.MISSED_TYPE;
    Cursor c = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, where, null, null);
    c.moveToFirst();
    Log.d("CALL COUNT: ", ""+c.getCount());
    c.close();
}

日志:

Call Logs permission: Not provided...

知道这个问题吗?

代码如下:

检查权限:

// Assume thisActivity is the current activity
    int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.READ_SMS);

以下代码检查应用程序是否有权读取用户的联系人,并且

必要时请求许可:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.READ_SMS)!= PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,Manifest.permission.READ_SMS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

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

        // MY_PERMISSIONS_REQUEST_READ_SMS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

输出:

official google doc & complete blog