如何获得双卡号码通话记录?

时间:2018-05-04 11:28:31

标签: android telephonymanager calllog dual-sim

如何在android中获取双卡号码呼叫日志。我按照下面的链接获取双SIM卡调用日志,但此方法始终返回-1。 我试过其他stackoverflow没有太多答案的双SIM卡呼叫日志,可从api 21获得。 https://stackoverflow.com/a/23907166/6891712

我尝试使用以下方法,该方法仅提供通话详细信息,但无法找到拨打或接听来电的SIM卡

private void getCalldetailsNow() {

         @SuppressLint("MissingPermission") Cursor managedCursor=c.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC");

        int number = 0;
        if (managedCursor != null) {
            number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
        }
        int duration1 = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
        int type1=managedCursor.getColumnIndex(CallLog.Calls.TYPE);
        int date1=managedCursor.getColumnIndex(CallLog.Calls.DATE);
        int idSimId = getSimIdColumn(managedCursor);

        if( managedCursor.moveToFirst() == true ) {
            String phNumber = managedCursor.getString(number);
            String callDuration = managedCursor.getString(duration1);

            String type=managedCursor.getString(type1);
            String date=managedCursor.getString(date1);
            String gettSimNumber=managedCursor.getString(idSimId);

            String dir = null;
            int dircode = Integer.parseInt(type);
            switch (dircode)
            { 
            case CallLog.Calls.OUTGOING_TYPE:
                dir = "OUTGOING";
                break;
            case CallLog.Calls.INCOMING_TYPE:
                dir = "INCOMING";
                break;
            case CallLog.Calls.MISSED_TYPE:
                dir = "MISSED";
                break;
            default: 
                dir = "MISSED";
                break;
            }       
        }

        managedCursor.close();
    }


public static int getSimIdColumn(final Cursor c) {

        for (String s : new String[] { "sim_id", "simid", "sub_id" }) {
            int id = c.getColumnIndex(s);
            if (id >= 0) {

                Log.d(" Simmmm", "sim_id column found: " + s);
                return id;
            }
        }
        Log.d(" Simmmm", "no sim_id column found");
        return -1;
    }

2 个答案:

答案 0 :(得分:0)

尝试使用此方法阅读SIM卡联系人..

如果你想阅读第一个设备商店联系人,那么实现实现LoaderManager.LoaderCallbacks 到类..

    @Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {

    return new CursorLoader(getActivity(),
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, ProfileQuery.PROJECTION,
            // Select only email addresses
            null, null, null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    List<String> numbers = new ArrayList<>();

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        String phoneNumber = cursor.getString(ProfileQuery.NUMBER).replaceAll("\\D", "");

        CustomLogHandler.printDebuglog(TAG, "onLoadFinished: " + phoneNumber);

        if (!TextUtils.isEmpty(phoneNumber) && !phoneNumber.startsWith(Constants.SP_COUNTRY_CODE)) {
            mCountryCodeList.add(new StringBuilder().append(Constants.SP_COUNTRY_CODE).toString());
            alPhoneNumbers.add(new StringBuilder().append(Constants.SP_COUNTRY_CODE).append(phoneNumber).toString());
        } else {
            mCountryCodeList.add(new StringBuilder().append(Constants.SP_COUNTRY_CODE).toString());
            alPhoneNumbers.add(phoneNumber);
        }
        names.add(cursor.getString(ProfileQuery.NAME));
        cursor.moveToNext();
    }
    allPhoneNumberName.addAll(names);
}

以下课程读取SIM卡数据..

private class ReadContactFromSIMCard extends AsyncTask<Object, Object, Object> {

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        requestForContact();
    }

    @Override
    protected Object doInBackground(Object[] params) {

        Cursor cursorSim = null;
        try {
            String phoneNo;
            String name;
            Uri simUri = Uri.parse("content://icc/adn");
            cursorSim = getActivity().getContentResolver().query(simUri, null, null, null, null);

            if (cursorSim != null) {
                while (cursorSim.moveToNext()) {
                    name = cursorSim.getString(cursorSim.getColumnIndex("name"));
                    phoneNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
                    if (!TextUtils.isEmpty(name)) {
                        allPhoneNumberName.add(name);
                    }
                    if (!TextUtils.isEmpty(phoneNo)) {
                        phoneNo.replaceAll("\\D", "");
                        phoneNo.replaceAll("&", "");

                        if (!TextUtils.isEmpty(phoneNo) && !phoneNo.startsWith(Constants.SP_COUNTRY_CODE)) {
                            mCountryCodeList.add(Constants.SP_COUNTRY_CODE);
                            alPhoneNumbers.add(Constants.SP_COUNTRY_CODE + phoneNo);
                        } else {
                            mCountryCodeList.add(Constants.SP_COUNTRY_CODE);
                            alPhoneNumbers.add(phoneNo);
                        }
                    }
                }
            }
        } catch (Throwable e) {
            CustomLogHandler.printErrorlog(e);
        } finally {
            if (cursorSim != null) {
                cursorSim.close();
            }
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        getLoaderManager().initLoader(0, null, FindFriendsFragment.this);
    }
}

获取通话记录详细信息......

private void getCallDetails() {
    StringBuffer sb = new StringBuffer();
    Uri contacts = CallLog.Calls.CONTENT_URI;
    Cursor managedCursor = this.getContentResolver().query(contacts, null, null, null, null);
    int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
    int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
    int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
    int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
    sb.append("Call Details :");
    while (managedCursor.moveToNext()) {

        HashMap rowDataCall = new HashMap<String, String>();

        String phNumber = managedCursor.getString(number);
        String callType = managedCursor.getString(type);
        String callDate = managedCursor.getString(date);
        String callDayTime = new Date(Long.valueOf(callDate)).toString();
        // long timestamp = convertDateToTimestamp(callDayTime);
        String callDuration = managedCursor.getString(duration);
        String dir = null;
        int dircode = Integer.parseInt(callType);
        switch (dircode) {
            case CallLog.Calls.OUTGOING_TYPE:
                dir = "OUTGOING";
                break;

            case CallLog.Calls.INCOMING_TYPE:
                dir = "INCOMING";
                break;

            case CallLog.Calls.MISSED_TYPE:
                dir = "MISSED";
                break;
        }
        sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
        sb.append("\n----------------------------------");
        Uri allCalls = Uri.parse("content://call_log/calls");
        Cursor c = managedQuery(allCalls, null, null, null, null);
        String id = c.getString(c.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID));
        Log.d("sim",id);


    }
    managedCursor.close();
    System.out.println(sb);
}

我希望你在android清单文件中添加以下权限..

    <uses-permission android:name="android.permission.READ_CALL_LOG" />

答案 1 :(得分:0)

也许您可以通过获取“Calls.PHONE_ACCOUNT_ID”项来获取subscription_id,该项称为“用于拨打或接听电话的帐户的标识符。”

在获得subscription_id后,例如,匹配插槽ID,插槽0是subscription_id 0,插槽ID 1是subscription_id 1,那么你可以使用giveb subid获取getSlotIndex,然后你可以使用这个slotid来查询调用从这个插槽或到这个插槽。