2016-11-25 1 views
0

Ich gab die READ_SMS sowie READ_CALL_LOG Erlaubnis in Manifest-Datei, aber immer noch bekomme ich SecurityException in meinem BroadcastReceiver.Permission Fehler in 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...

Jede Idee zu diesem Problem? Hier

+0

Geben Sie nicht READ_SMS aber READ_CALL_LOG Erlaubnis in manifest (Sie haben read_sms Erlaubnis in manifest hinzugefügt, aber in Laufzeitprüfung read_call_log Erlaubnis, bekam es? Dort sind Berechtigungen von verschiedenen Gruppen). Und wenn Sie READ_SMS Erlaubnis benötigen, dann sollten Sie es anfordern: https://developer.android.com/training/permissions/requesting.html https://developer.android.com/guide/topics/security/permissions.html # normal-gefährlich – krossovochkin

+0

Ich gab beide die Berechtigungen. Bitte sehen Sie meine bearbeitete Frage. – Satheshkumar

+1

@Satheshkumar lesen [this] (https://developer.android.com/guide/topics/security/permissions.html) vollständig. – Gattsu

Antwort

1

ist der Code:

prüfen Für Berechtigungen:

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

Der folgende Code überprüft, ob die App die Berechtigung hat die Kontakte des Benutzers zu lesen, und

fordert die Erlaubnis, falls erforderlich:

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

Ausgabe:

enter image description here

official google doc & complete blog

+0

Sie sind Danke! – Satheshkumar

+0

@Satheshkumar willkommen und glücklich Codierung :) – Gattsu