0

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.

Dubey.A
  • 89
  • 1
  • 1
  • 9

2 Answers2

0

There are few reasons the your BroadCastReceiver not running when app closed..

  1. Some mobiles not allow to run app in background like oppo,xiaomi,huawei etc.. to solve this issue try this Link
  2. The latest Android version Oreo Broadcast Limitations

if these reasons not part of your problem then use the service. How to do that? click

code4rox
  • 941
  • 9
  • 34
0

You should register your receiver implicitly in AndroidManifest.xml, for example like this:

<receiver
    android:name=".PhonecallReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    </intent-filter>
</receiver>
shmakova
  • 6,076
  • 3
  • 28
  • 44