Call logs for dual sim android device

大兔子大兔子 提交于 2019-12-01 00:23:33

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

Full path to this value CallLog.Calls.SUB_ID = "sub_id", but is not available for public, so just hardcode in API before 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 a fun :)

Here is the method which give you the All Call Logs Detail...

 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 don't forgot to add these permissions in Manifest.Xml

    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.WRITE_CALL_LOG" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!