在android中获取最后通话时长

前端之家收集整理的这篇文章主要介绍了在android中获取最后通话时长前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找一种最简单的方法来获得上次拨打号码的通话时长.因此,例如,如果我在拨打电话时打电话给我的妈妈,则应该会出现持续时间的通知.

我正在尝试以下但问题是它带有一个完整的持续时间列表.传入,传出,错过.

我如何区分它:

我尝试了以下方法

private void getCallDetails() {

    StringBuffer sb = new StringBuffer();
    Cursor cur = getContentResolver().query( CallLog.Calls.CONTENT_URI,null,android.provider.CallLog.Calls.DATE + " DESC");

    int number = cur.getColumnIndex( CallLog.Calls.NUMBER ); 
    int duration = cur.getColumnIndex( CallLog.Calls.DURATION);
    sb.append( "Call Details : \n");
    while ( cur.moveToNext() ) {
        String phNumber = cur.getString( number );
        String callDuration = cur.getString( duration );
        String dir = null;

        sb.append( "\nPhone Number:--- "+phNumber +" \nCall duration in sec :--- "+callDuration );
        sb.append("\n----------------------------------");
    }
    cur.close();
    call.setText(sb);
}

解决方法

试试以下代码
private void getCallDetails() {
        StringBuffer sb = new StringBuffer();
        Uri contacts = CallLog.Calls.CONTENT_URI;
        Cursor managedCursor = context.getContentResolver().query(contacts,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----------------------------------");


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

您将在下面的行中获得呼叫类型:

int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
原文链接:https://www.f2er.com/android/317417.html

猜你在找的Android相关文章