Call Logs for Dual SIM Devices

I use the code below to get call log details that work very well for a single SIM device, but the problem occurs when it comes to DUAL sim. I am trying to find a job for receiving logs from two SIM devices.

/** * Get All Call Logs details as JSON * * @param context */ @SuppressLint("SimpleDateFormat") private void getInitialCallDetailsAsJSON() { // Print dates of the current week starting on Monday DateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm aa", Locale.getDefault()); final Uri contacts = CallLog.Calls.CONTENT_URI; final Cursor managedCursor = getContentResolver().query(contacts, null, null, null, null); final int name = managedCursor .getColumnIndex(CallLog.Calls.CACHED_NAME); final int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER); final int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE); final int date = managedCursor.getColumnIndex(CallLog.Calls.DATE); final int durationOfCall = managedCursor .getColumnIndex(CallLog.Calls.DURATION); final JSONObject allDetailsJsonObject = new JSONObject(); final JSONArray array = new JSONArray(); if (managedCursor != null && managedCursor.getCount() > 0) { while (managedCursor.moveToNext()) { String contactName = managedCursor.getString(name); final String phoneNumber = managedCursor.getString(number); final String callTypeIndex = managedCursor.getString(type); final String callDate = managedCursor.getString(date); final String callDurationSeconds = managedCursor .getString(durationOfCall); final int totalTime = Integer.parseInt(callDurationSeconds); int day = (int) TimeUnit.SECONDS.toDays(totalTime); long hours = TimeUnit.SECONDS.toHours(totalTime) - (day * 24); long minute = TimeUnit.SECONDS.toMinutes(totalTime) - (TimeUnit.SECONDS.toHours(totalTime) * 60); long second = TimeUnit.SECONDS.toSeconds(totalTime) - (TimeUnit.SECONDS.toMinutes(totalTime) * 60); String callDurationFormatted = ""; if (hours < 10) { callDurationFormatted += "0" + hours + "h "; } else { callDurationFormatted += hours + "h "; } if (minute < 10) { callDurationFormatted += "0" + minute + "m "; } else { callDurationFormatted += minute + "m "; } if (second < 10) { callDurationFormatted += "0" + second + "s"; } else { callDurationFormatted += second + "s"; } String callType = null; final int dircode = Integer.parseInt(callTypeIndex); switch (dircode) { case CallLog.Calls.OUTGOING_TYPE: callType = CallAnalyticsConstant.OUTGOING; break; case CallLog.Calls.INCOMING_TYPE: callType = CallAnalyticsConstant.INCOMING; break; case CallLog.Calls.MISSED_TYPE: callType = CallAnalyticsConstant.MISSED; break; } if (contactName == null || contactName.equalsIgnoreCase("")) { contactName = "UNKNOWN"; } } managedCursor.close(); } 

If any of you are thinking, please kindly help me get the details related to the call for two SIM phones.

APP, which works great for a dual SIM phone, - CALL MONITOR <

+2
source share
2 answers

You can use the constant value "sub_id" to get information about the SIM card.

The full path to this value is CallLog.Calls.SUB_ID = "sub_id", but is not available to the public, so there’s just a hard code in the API until 21. For> = 21 you can use PHONE_ACCOUNT_COMPONENT_NAME.

  /** * The subscription ID used to place this call. This is no longer used and has been * replaced with PHONE_ACCOUNT_COMPONENT_NAME/PHONE_ACCOUNT_ID. * For ContactsProvider internal use only. * <P>Type: INTEGER</P> * * @Deprecated * @hide */ public static final String SUB_ID = "sub_id"; 

Have fun :)

+3
source

Here is a method that gives you information about all call logs ...

  private void getCallDetails() { StringBuffer sb = new StringBuffer(); Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,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() ) { String phNumber = managedCursor.getString( number ); String callType = managedCursor.getString( type ); String callDate = managedCursor.getString( date ); Date callDayTime = new Date(Long.valueOf(callDate)); 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----------------------------------"); } managedCursor.close(); System.out.println(sb.toString()); } 

And also do not forget to add these permissions to Manifest.Xml

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

Source: https://habr.com/ru/post/1393047/


All Articles