1

我怎样才能找到最后一个打给我的“号码”?(接听电话)

请不要回答以下 cod 它有错误(因为类扩展PhoneStateListener并且方法需要Activity扩展:

    public class CallStat extends PhoneStateListener {

String LOG_TAG = "calllog";
private boolean isPhoneCalling = false;

@Override
public void onCallStateChanged(int state, String incomingNumber) {

    if (TelephonyManager.CALL_STATE_RINGING == state) {
        // phone ringing
        Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
    }

    if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
        // active
        Log.i(LOG_TAG, "OFFHOOK");

        isPhoneCalling = true;
    }

    if (TelephonyManager.CALL_STATE_IDLE == state) {
        // run when class initial and phone call ended, need detect flag
        // from CALL_STATE_OFFHOOK
        Log.i(LOG_TAG, "IDLE number");

        if (isPhoneCalling) {

            Handler handler = new Handler();

            //Put in delay because call log is not updated immediately when state changed
            // The dialler takes a little bit of time to write to it 500ms seems to be enough
            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    // get start of cursor
                      Log.i("CallLogDetailsActivity", "Getting Log activity...");
                        String[] projection = new String[]{Calls.NUMBER};
                        Cursor cur = getContentResolver().query(Calls.CONTENT_URI, projection, null, null, Calls.DATE +" desc");
                        cur.moveToFirst();
                        String lastCallnumber = cur.getString(0);
                }
            },500);

            isPhoneCalling = false;
        }

    }
}
}

我知道传出,但传入呢?

4

2 回答 2

3

我相信上一个答案中的代码有点错误。

方法中的第一个参数query应该是一个 URI。

请参阅以下更正:

        //fields to select.
        String[] strFields = {
                android.provider.CallLog.Calls.NUMBER,
                android.provider.CallLog.Calls.TYPE,
                android.provider.CallLog.Calls.CACHED_NAME,
                android.provider.CallLog.Calls.CACHED_NUMBER_TYPE
        };

        //only incoming.
        String strSelection = android.provider.CallLog.Calls.TYPE + " = " + android.provider.CallLog.Calls.INCOMING_TYPE;       

        //most recent first
        String strOrder = android.provider.CallLog.Calls.DATE + " DESC"; 


        //get a cursor.
        Cursor mCallCursor = getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI, //content provider URI
            strFields, //project (fields to get)
            strSelection, //selection
            null, //selection args
            strOrder //sortorder.
        );

         mCallCursor.moveToFirst();
于 2013-08-30T10:10:03.430 回答
1
String[] strFields = {
    android.provider.CallLog.Calls.NUMBER, 
    android.provider.CallLog.Calls.TYPE,
    android.provider.CallLog.Calls.CACHED_NAME,
    android.provider.CallLog.Calls.CACHED_NUMBER_TYPE
    };
String strOrder = android.provider.CallLog.Calls.DATE + " DESC"; 

Cursor mCallCursor = getContentResolver().query(
    android.provider.CallLog.Calls.INCOMING_TYPE,
    strFields,
    null,
    null,
    strOrder
    );

 mCallCursor.moveToFirst();

这将返回call log拨出电话。

或使用您的代码:

Cursor cur = getContentResolver().query( android.provider.CallLog.Calls.INCOMING_TYPE, projection, null, null, Calls.DATE +" desc");
cur.moveToFirst();
String lastCallnumber = cur.getString(0);
于 2013-08-17T17:42:54.780 回答