I have been working on an app that can detect incoming & outgoing calls in android and record them. I need to know which of my sim receives a call or make a call.
I can detect calls but unable to detect my number in case of dual sim. I have tried to check it from my call logs.
Here is my code:
private void fetchCallLogs(final Context context) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// get start of cursor
Log.i("CallLogDetailsActivity", "Getting Log activity...");
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Cursor cur = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " desc");
int accountId = cur.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID);
//Check if call was made from sim 1 or sim 2 , if it returns 0 its from sim 1 else if 1 its from sim 2.
String callid = "0";
if (cur.moveToFirst() == true) {
String phNumber = cur.getString(accountId);
int idSimId = getSimIdColumn(cur);
if(idSimId >= 0){
callid = cur.getString(idSimId);
}
cur.close();
}
}
}, 1500);
}
public static int getSimIdColumn(final Cursor c) {
for (String s : new String[] { "sim_id", "simid","simId","simSlot", "sub_id","slot","simnum","slotId","slot_id","slotIdx" }) {
int id = c.getColumnIndex(s);
if (id >= 0) {
Log.d("TAG", "sim_id column found: " + s);
return id;
}
}
Log.d("TAG", "no sim_id column found");
return -1;
}
also tried to detect if using detecting sim slot
String number = intent.getExtra("slot",-1);
but this doesn't work.
I need a code that can detect either sim number or slot.
Thanks in advance.