'How to access CALL LOG Permission Android 11 (API 30)?
I need to acces Android (API 30) CALL LOG, from my app, I have this method below, but throws the exception, requires the permisson : android.permission.READ_CALL_LOG or android.permission.WRITE_CALL_LOG
This is the Manifest
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
This us the function Java:
public static String obtenerDetallesLlamadas(Context context) {
StringBuffer stringBuffer = new StringBuffer();
Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI,
null, null, null, CallLog.Calls.DATE + " DESC");
int number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = cursor.getColumnIndex(CallLog.Calls.TYPE);
int date = cursor.getColumnIndex(CallLog.Calls.DATE);
int duration = cursor.getColumnIndex(CallLog.Calls.DURATION);
while (cursor.moveToNext()) {
String phNumber = cursor.getString(number);
String callType = cursor.getString(type);
String callDate = cursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = cursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
stringBuffer.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
+ dir + " \nCall Date:--- " + callDayTime
+ " \nCall duration in sec :--- " + callDuration);
stringBuffer.append("\n----------------------------------");
}
cursor.close();
return stringBuffer.toString();
}
Solution 1:[1]
With this permissons with the method below Android read the Manifest and works from Android 6+
public static boolean comprobar_Permisos_CALL_LOG(Context myContext, Activity myActivity){
try {
// Check whether this app has the call log allowed
int writeExternalStoragePermission = ContextCompat.checkSelfPermission(myContext, Manifest.permission.READ_CALL_LOG);
if(writeExternalStoragePermission!= PackageManager.PERMISSION_GRANTED)
{
// Request user to grant write external storage permission.
ActivityCompat.requestPermissions(myActivity, new String[]{
Manifest.permission.READ_CALL_LOG
}, REQUEST_CODE_CALL_LOG_PERMISSION);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R ) {
try {
Intent intent = new Intent();
Uri uri = Uri.fromParts("package", myActivity.getPackageName(), null);
intent.setData(uri);
myActivity.startActivityForResult(intent,REQUEST_CODE_CALL_LOG_PERMISSION);
return false;
} catch (Exception e) {
return false; //if anything needs adjusting it would be this
}
} else {
return false;
}
}else {
return true;
}
}catch (Exception ex)
{
Log.e(LOG_TAG_EXTERNAL_STORAGE, ex.getMessage(), ex);
}
return false;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | inec |

