4

I am trying to get sim name for each call log like this one:

enter image description here

Uri URI_PHONE = CallLog.Calls.CONTENT_URI;
    String SELECTION_PHONE = CallLog.Calls.NUMBER + "=?";
    String[] SELECTION_ARRAY_PHONE = new String[]{phNum};

    if (ActivityCompat.checkSelfPermission(getContext(), android.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.
        Toast.makeText(getContext(), "Grant Permission First", Toast.LENGTH_SHORT).show();
    }
    Cursor currLogs = getContext().getContentResolver().query(URI_PHONE, null, SELECTION_PHONE, SELECTION_ARRAY_PHONE, CallLog.Calls.DATE + " DESC;");

    int duration = currLogs.getColumnIndex(CallLog.Calls.DURATION);
    int acountName = currLogs.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID);
    int date = currLogs.getColumnIndex(CallLog.Calls.DATE);
    int type = currLogs.getColumnIndex(CallLog.Calls.TYPE);

    if (currLogs.getCount()>0) {
        while (currLogs.moveToNext()) {
            String talkedFor = currLogs.getString(duration);
            String acount = currLogs.getString(acountName);
            String dt = currLogs.getString(date);
            String calltp = currLogs.getString(type);
            int tp = Integer.parseInt(calltp);

            Drawable drawableType = null;
            switch (tp) {
                case CallLog.Calls.OUTGOING_TYPE:
                    drawableType = out;
                    break;

                case CallLog.Calls.INCOMING_TYPE:
                    drawableType = in;
                    break;

                case CallLog.Calls.MISSED_TYPE:
                    drawableType = missed;
                    break;

            }

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
                SubscriptionManager subscriptionManager = (SubscriptionManager) getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);

                List<SubscriptionInfo> subscriptionInfoList = null;

                subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();

                if (subscriptionInfoList != null && subscriptionInfoList.size() > 0) {
                    for (SubscriptionInfo info : subscriptionInfoList) {
                        String carrierName = String.valueOf(info.getDisplayName());
                        String mobileNo = info.getNumber();
                        String countyIso = info.getCountryIso();
                        String id = info.getIccId();
                        int dataRoaming = info.getDataRoaming();
                        if (id.equals(acount)) {
                            if (carrierName.isEmpty()){
                                simCardName = (String) info.getDisplayName();
                            }else {
                                simCardName = carrierName;
                                break;
                            }
                        }
                    }

                }
            }

            callLogsMore.add(new CallsDetailsModel(talkedFor, drawableType,simCardName, dt));
        }
    }

I have tested this code on Lenovo A7700 running Android 6.1 and it worked fine but when testing in other devices like Redmi Note 4 it showed an empty name. I have also tried telephony manager solution but it wasn't working in my phone (Lenovo A7700) so i choose this procedure.

I think the solution is something related to do with CallLogs.Calls.PHONE_ACCOUNT_COMPONENT_NAME. when querying for this, it returns a long string value.

Please help me. I've been stuck here for more than a month.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Akash Sahu
  • 171
  • 1
  • 9

0 Answers0