9

我正在寻找一种最简单的方法来获取最后拨打的号码的通话时间。因此,例如,如果我在切断通话后给妈妈打了电话,则应该会出现带有持续时间的通知。

我正在尝试以下方法,但问题是它带有完整的持续时间列表。进来的,出去的,错过的。

我如何区分它:

我尝试了以下方法:

 private void getCallDetails() {

    StringBuffer sb = new StringBuffer();
    Cursor cur = getContentResolver().query( CallLog.Calls.CONTENT_URI,null, null,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);
}
4

9 回答 9

13

您需要在内容查询中使用限制子句来获取最后通话详细信息。所以你的内容查询会变成

Cursor cur = getContentResolver().query( CallLog.Calls.CONTENT_URI,
             null, null, null, android.provider.CallLog.Calls.DATE + " DESC limit 1;");
于 2015-12-31T09:22:06.970 回答
12

试试下面的代码:

private void getCallDetails() {
        StringBuffer sb = new StringBuffer();
        Uri contacts = CallLog.Calls.CONTENT_URI;
        Cursor managedCursor = context.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----------------------------------");


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

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

int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
于 2013-10-21T11:36:39.797 回答
6

这是我的代码,非常适合最后一次拨出电话。

private String 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);

    managedCursor.moveToLast();
        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;
        }

    if(dir.equals("OUTGOING")){
       //whatever you want here
        return "yes";
    }

    managedCursor.close();
    return "no";

}
于 2016-02-19T14:54:26.920 回答
3

获取最后通话时间,而不是以前尝试延迟

private Handler finishedCall = new Handler();    
finishedCall.postDelayed(new Runnable() {
        @Override
        public void run() {
            String dura = LastCall();
    }, 1000);

并调用 Lastcall 函数

public String LastCall() {
            String callDura = "0";
            StringBuffer sb = new StringBuffer();
            Uri contacts = CallLog.Calls.CONTENT_URI;
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
                callDura = null;
            }
            else {
                Cursor managedCursor = getApplicationContext().getContentResolver().query(
                        contacts, null, null, null, android.provider.CallLog.Calls.DATE + " DESC limit 1;");
                int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
                int duration1 = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
                int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
                if (managedCursor.moveToFirst() == true) {
                    String phNumber = managedCursor.getString(number);
                    callDura = managedCursor.getString(duration1);
                    String callDate = managedCursor.getString(date);
                    String callDayTime = new Date(Long.valueOf(callDate)).toString();
                    String dir = null;
                    Log.e("DUR", "\nPhone Number:--- " + phNumber + " \nCall duration in sec :--- " + callDura + " \nCall Date in sec :--- " + callDayTime);
                }
                managedCursor.close();
            }
            return callDura;
     }
于 2018-05-17T06:15:14.237 回答
2

这段代码对我来说很好......

void retriveCallSummary() {

            Log.i("*****retriveCallSummary******","Call retrive method worked");
            StringBuffer sb = new StringBuffer();
            Uri contacts = CallLog.Calls.CONTENT_URI;
            Cursor managedCursor = mContext.getContentResolver().query(
                    contacts, null, null, null, null);
            int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER ); 
            int duration1 = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
            if( managedCursor.moveToFirst() == true ) {
                String phNumber = managedCursor.getString( number );
                String callDuration = managedCursor.getString( duration1 );
                String dir = null;
                sb.append( "\nPhone Number:--- "+phNumber +" \nCall duration in sec :--- "+callDuration );
                sb.append("\n----------------------------------");
                    Log.i("*****Call Summary******","Call Duration is:-------"+sb);
            }
            managedCursor.close();
        }
于 2014-02-06T07:18:13.837 回答
1

这段代码我用了 100%

public String LastCall() {
        StringBuffer sb = new StringBuffer();
        Cursor cur = getContentResolver().query( CallLog.Calls.CONTENT_URI,null, null,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 );
            sb.append( "\nPhone Number:"+phNumber);
            break;
        }
    cur.close();
    String str=sb.toString();
    return str;
}
于 2015-12-28T02:03:22.857 回答
0

这是获取最后通话时间的代码。试试吧,它有效。最后一次通话时长是从通话记录中获取的。

  public void getCallLog() {
    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 :");
    Log.e("total count", "" + managedCursor.getCount());
    //managedCursor.moveToPosition(managedCursor.getCount() - 1);
    int currentCount = 0, lastPosition = 0;
    while (managedCursor.moveToNext()) {
        currentCount++;
        //managedCursor.moveToPosition(managedCursor.getCount() - 1);
        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:
              //  lastPosition = currentCount;
                dir = "OUTGOING";
                break;

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

            case CallLog.Calls.MISSED_TYPE:
                dir = "MISSED";
                break;

        }
        lastPosition = currentCount;
        sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
        sb.append("\n----------------------------------");
        Log.e("test", sb.toString());
    }

    lastPosition--;
    managedCursor.moveToPosition(lastPosition);
    int requiredNumber = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
    int durations = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
    String phNumber = managedCursor.getString(requiredNumber);
    String dur = managedCursor.getString(durations);

    textView.setText(phNumber);
    textDuration.setText(dur);
    Log.e("last position number ", phNumber);
    Log.e("last call Duration ", dur);
    managedCursor.close();
}
于 2017-01-19T11:12:12.407 回答
0
public class Home extends Activity {
    TextView textView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);
        textView = (TextView) findViewById(R.id.textview_call);
        getCallDetails();
    }

    private void getCallDetails() {

        Context context;
        StringBuffer sb = new StringBuffer();
        Uri contacts = CallLog.Calls.CONTENT_URI;
        try {
            Cursor managedCursor = getContentResolver().query( CallLog.Calls.CONTENT_URI,null, null,null, android.provider.CallLog.Calls.DATE + " DESC limit 1;");
            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);
            textView.setText(sb);
        }
        catch (SecurityException e)
        {
            System.out.println();
             // lets the user know there is a problem with the code
        }




    }
}
于 2017-09-20T17:05:23.753 回答
0

使用以下排序顺序:

CallLog.Calls.DATE " + " + CallLog.Calls.DURATION + " * 1000 desc "
于 2018-03-14T22:07:57.440 回答