0

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?

Satheshkumar
  • 232
  • 2
  • 12
  • Provide not READ_SMS but READ_CALL_LOG permission in manifest (you've added read_sms permission in manifest, but in runtime check read_call_log permission, got it? There permissions are of different groups). And if you need READ_SMS permission, then you should request it: https://developer.android.com/training/permissions/requesting.html https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous – krossovochkin Nov 25 '16 at 13:20
  • I gave both the permissions. Please see my edited question. – Satheshkumar Nov 25 '16 at 13:22
  • 1
    @Satheshkumar read [this](https://developer.android.com/guide/topics/security/permissions.html) completely. – Maveňツ Nov 25 '16 at 13:24
  • @maveň Thanks for the link. – Satheshkumar Nov 25 '16 at 13:25
  • https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it – CommonsWare Nov 25 '16 at 13:28
  • @Satheshkumar [READ_CALL_LOG](https://developer.android.com/reference/android/Manifest.permission.html#READ_CALL_LOG) & [READ_SMS](https://developer.android.com/reference/android/Manifest.permission.html#READ_SMS) both have different usage and role they are meant for. you have only used READ_CALL_LOG permission ... adding permission to manifest won't work for api>23 – Maveňツ Nov 25 '16 at 13:29
  • @maveň 'adding permission to manifest won't work for api>23' This gave me the answer. Thanks. – Satheshkumar Nov 25 '16 at 13:31

1 Answers1

1

Here is the code:

Check For Permissions:

// 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

Requests the permission if necessary:

// 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:

enter image description here

official google doc & complete blog

Community
  • 1
  • 1
Maveňツ
  • 1
  • 12
  • 50
  • 89