Вы должны получить разрешения, как показано ниже
<uses-permission android:name="android.permission.READ_CALL_LOG"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"></uses-permission>
Используйте этот код для получения журналов вызовов
if (Build.VERSION.SDK_INT >= 23) {
requestPermissions(new String[] {
Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_CALL_LOG
}, 200);
}
Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);
while (c.moveToNext()) {
String num = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER)); // for number
String name = c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME)); // for name
String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION)); // for duration
int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));
Log.e("TEST", num);
}
Если вы хотите получить его в списке
ArrayList < String > list = new ArrayList < > ();
if (Build.VERSION.SDK_INT >= 23) {
requestPermissions(new String[] {
Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_CALL_LOG
}, 200);
}
Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);
while (c.moveToNext()) {
String num = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER)); // for number
String name = c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME)); // for name
String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION)); // for duration
int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));
list.add(num);
}
final String[] listAccounts = list.toArray(new String[0]);
final int[] currentApp = {
0
};
// Dialog box for multiple card accounts
final AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
//mBuilder.setTitle("Choose an account :");
mBuilder.setTitle(Html.fromHtml("<font color='#FF7F27'>Num</font>"));
mBuilder.setCancelable(false);
mBuilder.setSingleChoiceItems(listAccounts, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e("Debug_", "Clicked: " + listAccounts[i]);
currentApp[0] = i;
}
});
mBuilder.setPositiveButton("Select", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
Log.e("Debug_", "Name: " + listAccounts[currentApp[0]] + " Index: " + currentApp[0]);
number.setText(listAccounts[currentApp[0]]);
dialogInterface.dismiss();
}
});
AlertDialog mDialog = mBuilder.create();
mDialog.show();