2016-08-14 36 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);