2016-08-14 44 views
2

我有下面的代碼,以獲得我的應用程序的手機狀態:的Android PhoneStateListener錯誤狀態

private PhoneStateListener phoneListener = new PhoneStateListener(){ 
    public void onCallStateChanged(int state,String incomingNumber){ 
     switch(state){ 
      case TelephonyManager.CALL_STATE_IDLE: 
       Log.i(TAG, "IDLE"); 
       break; 
      case TelephonyManager.CALL_STATE_OFFHOOK: 
       Log.i(TAG, "OFFHOOK"); 
       break; 
      case TelephonyManager.CALL_STATE_RINGING: 
       Log.i(TAG, "RINGING"); 
       break; 
     } 
    } 
}; 

所有工作在兩個仿真器(在Android 6.0和Android 4.4)的罰款。但我真正的設備上(Android 4.4系統,Oukitel原)我已經得到了以下輸出,同時振鈴:

RINGING 
IDLE //in real I'm still ringing here. 
IDLE //real stop ringing 

是否有任何狡猾的功能,或者它只是一個錯誤的固件?不幸的是,我的搜索沒有任何結果。

UPD 它適用於某些設備,對於其他設備,它的工作方式如上所述。

一些更多的細節。我在我的服務的onStartCommand中設置了此偵聽器。如果TelephonyManager.EXTRA_STATE_RINGING得到了注意,那麼它將從廣播接收器開始。

UPD2 嗯,我找到了一個解決方案。廣播接收器工作正常,所以我剛切換到Receiver通知。但是真的很有趣,PhoneStateListener出了什麼問題。如果有人有任何想法,請告訴我。

回答

0

我能夠通過讓服務來解決這個問題上4.4做出startForeground通知,同時打電話給保持服務的活力。您的問題可能在其他地方,但可能值得嘗試一下。

在我服務的onCreate:

Intent notificationIntent = new Intent(this, MainActivity.class); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 
      notificationIntent, 0); 

    Notification notification = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_icon1) 
      .setContentTitle("title") 
      .setContentText("description") 
      .setContentIntent(pendingIntent).build(); 

    startForeground(1337, notification);