我在清单文件中提供了READ_SMS
和READ_CALL_LOG
权限,但我仍然在SecurityException
中获得了BroadcastReceiver
。
代码:
@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...
对这个问题有任何想法吗?
答案 0 :(得分:1)
以下是代码:
// 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.
}
}
输出: