3

我的廣播獲取調用,但PhoneStateListener不叫我的代碼工作正常,當我在模擬器中運行,但是當我嘗試在實際設備PhoneStateListener從來沒有得到一個電話,我對這個nutts去問題 這裏是我的代碼:PhoneStateListener永遠不會在廣播機器人叫做

TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object 
          CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener(); 
          telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); //Register our listener with TelephonyManager 

上面的代碼中的onReceive()方法,這裏是我的課extensing PhoneStateListener 的onReceive()之外,但在廣播類。

public class CustomPhoneStateListener extends PhoneStateListener { 

     private static final String TAG = "CustomPhoneStateListener"; 



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

      if(phonenumber!=null && phonenumber.length()>0) 
       incoming_nr=phonenumber; 
      act=new Call_RecorderActivity(); 

      switch(state){ 
       case TelephonyManager.CALL_STATE_RINGING: 
         Log.d(TAG, "CALL_STATE_RINGING"); 
         prev_state=state; 


         break; 


       case TelephonyManager.CALL_STATE_OFFHOOK: 
       Log.d(TAG, "CALL_STATE_OFFHOOK"); 
       prev_state=state; 

       break; 


       case TelephonyManager.CALL_STATE_IDLE: 
        prev_state=state; 
        Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr); 



        break; 
        // Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT 
        // |Intent.FLAG_ACTIVITY_REORDER_TO_FRONT 

      } //switch close 
     } 
} 

logcat中顯示了來電

01-22 11:25:12.529: I/IncomingCallReceiver(1463): Bundle[mParcelledData.dataSize=168] 
01-22 11:25:12.539: I/IncomingCallReceiver(1463): State: RINGING 
01-22 11:25:12.539: I/IncomingCallReceiver(1463): Incomng Number: +9184848xxxx2 
01-22 11:25:12.779: D/CustomPhoneStateListener(1463): CALL_STATE_IDLE==>+9184848xxxx2 
01-22 11:25:16.299: I/IncomingCallReceiver(1463): Bundle[mParcelledData.dataSize=92] 
01-22 11:25:16.299: I/IncomingCallReceiver(1463): State: OFFHOOK 
01-22 11:25:18.849: I/IncomingCallReceiver(1463): Bundle[mParcelledData.dataSize=88] 
01-22 11:25:18.849: I/IncomingCallReceiver(1463): State: IDLE 

與其相關的任何建議將提前

+0

確保你已經加入''android.permission.READ_PHONE_STATE許可,不得以艙單 –

+0

是的,我補充說,允許 –

+0

告訴我們你的日誌和貓 – jenuine

回答

2

您的服務是越來越清理回調函數調用之前被接受 感謝這一點。在onReceive退出後,您不應該依賴BroadcastReceiver中分配的任何內容。您應該在ServiceActivity中輸入CustomPhoneStateListener。然後,您可以使用Intent啓動活動或服務來進行狀態監控。

BroadcastReceiver文檔,

一旦你的onReceive()返回時,廣播接收器不再 活躍,它的宿主進程只是作爲在它運行的任何其他 應用程序組件一樣重要。尤其是 重要,因爲如果該過程僅託管 BroadcastReceiver(對於用戶具有 從未或最近未與之交互過的應用程序的常見情況),則在從 返回onReceive()時,系統將認爲其進程是空和 積極殺死它,使資源可用於其他更多 重要的過程。

它在仿真器上工作的原因可能是因爲仿真器上的進程總體較少,出於某種原因,它在關閉進程方面似乎較不積極。

+0

@ Auto-Droid它在模擬器上工作的原因可能是因爲模擬器上的進程通常較少,並且出於某種原因,它在關閉進程方面似乎較不積極。因此,您的進程仍在模擬器的內存中,但不在設備上。它不會改變這樣一個事實,即根據文檔,您不能保證該流程將不會被保留。因此,上面寫的代碼可能會或可能不會對任何給定的調用起作用。 – iagreen

+0

我接受了可以檢測到手機狀態的服務,但我的服務被調用,但仍然存在我的聽衆沒有接到呼叫的問題仍然存在 –

+0

您想要啓動服務的廣播意圖是什麼?我能夠將你的代碼複製到'Service'的'onStartCommand'中,並且它在真實設備上記錄了所有狀態消息。 – iagreen

0

對上述問題裏邊反的解決方案是:

private final PhoneStateListener phoneStateListener = new PhoneStateListener() { 

    @Override 

    public void onCallStateChanged(int state, String incomingNumber) { 

     String callState = "UNKNOWN"; 

     String myNumber = tm.getLine1Number(); 
     switch (state) { 

     case TelephonyManager.CALL_STATE_IDLE: 

      callState = "IDLE"; 
      if(Status!=""){ 
       Toast.makeText(mContext,"Call Ends " + incomingNumber,Toast.LENGTH_LONG).show(); 
      } 
      break; 

     case TelephonyManager.CALL_STATE_RINGING: 
      Status = "RINGING"; 
      if (incomingNumber.startsWith("00")) { 
       Toast.makeText(mContext,"International Call- " + incomingNumber,Toast.LENGTH_LONG).show(); 
       callState = "International - Ringing (" + incomingNumber+ ")"; 
      } else { 
       Toast.makeText(mContext, "Local Call - " + incomingNumber, Toast.LENGTH_LONG).show(); 
       callState = "Local - Ringing (" + incomingNumber + ")"; 
      } 
      break; 
     case TelephonyManager.CALL_STATE_OFFHOOK: 

      try{ 
       String dialingNumber = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
       if(dialingNumber==null){ 
        Status = "Recieve"; 
        Toast.makeText(mContext, "Recieve call", Toast.LENGTH_LONG).show(); 
       }else{ 
        Status = "Dialing"; 
        if (dialingNumber.startsWith("00")) { 
         Toast.makeText(mContext,"International - " + dialingNumber,Toast.LENGTH_LONG).show(); 
         callState = "International - Dialing (" + dialingNumber+ ")"; 
        } else { 
         Toast.makeText(mContext, "Local Call - " + dialingNumber,Toast.LENGTH_LONG).show(); 
         callState = "Local - Dialing (" + dialingNumber + ")"; 
        } 
       } 
      }catch(Exception e){} 

      break; 

     } 

     Log.i(">>>Broadcast", "onCallStateChanged " + callState); 

     super.onCallStateChanged(state, incomingNumber); 

    } 

};