Reputation: 232
I gave the READ_SMS
as well as READ_CALL_LOG
permission in manifest file but still I am getting SecurityException
in my BroadcastReceiver
.
Code:
@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();
}
Log:
Call Logs permission: Not provided...
Any idea about this problem?
Upvotes: 0
Views: 643
Reputation: 1
Here is the code:
// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.READ_SMS);
The following code checks if the app has permission to read the user's contacts, and
// 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.
}
}
Output:
official google doc & complete blog
Upvotes: 1