2013-08-17 68 views
1

我怎樣才能找出最後進入的「數量」是叫我嗎?(接電話)如何獲得最後的「收到」電話號碼?

請不要回答以下鱈魚 他有一個錯誤(因爲該類擴展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; 
     } 

    } 
} 
} 

我知道傳出但對於傳入

回答

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); 
+0

我需要一種用於「進入」調用 – AlizerA

+0

上面的代碼是用於返回來電分貝內容。你甚至看到我的答案?! –

+0

Cursor cur = getContentResolver()。query(android.provider.CallLog.Calls.INCOMING_TYPE,projection,null,null,Calls.DATE +「desc」); //語法錯誤 – AlizerA

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();