2009-12-05 66 views
42

來電的電話號碼,我想找回來電的PHONENUMBER並用它做什麼像做 在http://blog.whitepages.com/2009/02/27/caller-id-by-whitepages-a-new-android-app-that-puts-telemarketers-on-alert/檢索的Android

能否請你幫我,因爲我無法找到有關這方面的消息。 我從哪裏開始,如何獲取語數?


好的,所以目前我的代碼如下所示。當我將呼叫置於CustomBroadcastReceiver中時,它會打印出日誌消息。我可以從包中找回電話號碼。但!我無法使CustomPhoneStateListener工作。正如你所看到的,我已將自定義電話狀態偵聽器註冊到接收器,但日誌消息從未從CustomPhoneStateListener類打印出來。我在這裏錯過了什麼? 我的想法是否正確?


<receiver android:name=".CustomBroadcastReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.PHONE_STATE" /> 
     </intent-filter> 
</receiver> 

</application> 
<uses-sdk android:minSdkVersion="5" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_CONTACTS" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 

public class CustomPhoneStateListener extends PhoneStateListener { 

private static final String TAG = "CustomPhoneStateListener"; 

public void onCallStateChange(int state, String incomingNumber){ 

    Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!"); 
    Log.v(TAG, incomingNumber); 

    switch(state){ 
     case TelephonyManager.CALL_STATE_RINGING: 
      Log.d(TAG, "RINGING"); 
      break; 
    } 
} 

public class CustomBroadcastReceiver extends BroadcastReceiver { 

private static final String TAG = "CustomBroadcastReceiver"; 

@Override 
public void onReceive(Context context, Intent intent) { 
    Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!"); 
    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
    CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener(); 

    telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); 


    Bundle bundle = intent.getExtras(); 
    String phoneNr= bundle.getString("incoming_number"); 
    Log.v(TAG, "phoneNr: "+phoneNr); 

} 

回答

25

使用PhoneStateListener。它有一個onCallStateChanged處理程序;提供的參數之一是包含傳入電話號碼的String

+0

能否請您提供一個例子實施? – jakob

+0

我對這個問題的回答可能有幫助[鏈接](http://stackoverflow.com/questions/10136490/androidget-phone-number-of-present-incoming-and-outgoing-call/11182720#11182720) – Gary

5

您在CustomPhoneStateListener中重寫的方法應稱爲onCallStateChanged()(而不是onCallStateChange())。

如果您有@Override註釋,就會發現這是Java編譯器發現的問題,就像您對onReceive()的註釋一樣。

+0

你是人!謝謝!我還有一個問題需要問,那就是我如何向來電視圖添加內容? – jakob

+1

我不確定那件事的細節。你的錯字更容易:)但是,我認爲你正在尋找的是一個吐司。但它可能不會顯示在來電屏幕上,除非您的代碼是服務(而不是活動)。但正如我所說,我自己還沒有在Android開發中得到這麼多。 http://developer.android.com/guide/topics/ui/notifiers/toasts.html – mikeplate

+0

好酷,謝謝! – jakob

9

這是一個實現,它將允許您檢索電話號碼,如果它是傳入的電話呼叫爲incomingNumber,並且還有呼叫完成時 - 請注意Handler()代碼。

private class PhoneCallListener extends PhoneStateListener { 

    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; 
      } 

     } 
    } 
} 

,然後添加和初始化你的onCreate聽衆或onStartCommand代碼:

PhoneCallListener phoneListener = new PhoneCallListener(); 
    TelephonyManager telephonyManager = (TelephonyManager) this 
      .getSystemService(Context.TELEPHONY_SERVICE); 
    telephonyManager.listen(phoneListener, 
      PhoneStateListener.LISTEN_CALL_STATE);