如何获取Android中通话记录项的SIM卡名称?

时间:2018-07-19 17:48:00

标签: java android android-studio calllog dual-sim

我正在尝试为每个通话记录获取SIM卡名称,如下所示:

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));
        }
    }

我已经在运行Android 6.1的Lenovo A7700上测试了此代码,并且工作正常,但是在其他设备(如Redmi Note 4)中进行测试时,它显示了一个空名称。我还尝试了电话管理器解决方案,但在我的手机(联想A7700)中无法使用,因此我选择此过程。

我认为解决方案与CallLogs.Calls.PHONE_ACCOUNT_COMPONENT_NAME有关。查询时,它返回一个长字符串值。

请帮助我。我已经在这里停留了一个多月。

0 个答案:

没有答案