如何获得最后一个“收到”的电话号码?

时间:2013-08-17 17:06:04

标签: android phone-call

我怎样才能找到最后拨打给我的“号码”?(已接来电)

请不要回答以下鳕鱼 它有一个错误(因为类扩展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;
        }

    }
}
}

我知道传出但是传入怎么办?

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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);