PhoneStateListener用于呼叫接收器不在真实设备中工作

时间:2014-07-16 05:49:39

标签: android broadcastreceiver telephonymanager phone-state-listener

我创建了BroadCastReceiver,用于收听IncomingCallOutgoing Calls。试图在PhoneStateListener中添加TelephonyManager BroadCastReceiver,该版本在模拟器和某些设备上运行良好。

它不适用于Samsung ST 7652。始终返回CALL_STATE_IDLE和来电号null。为什么会这样?

如下:

public class ReceiverInComingCall extends BroadcastReceiver { 
   public void onReceive(Context context, Intent intent) {
    TelephonyManager phone = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
    phone.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
   }

  private class MyPhoneStateListener extends PhoneStateListener{
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);

        switch(state){
        case TelephonyManager.CALL_STATE_RINGING:
         Log.i("CALL","CALL_STATE_RINGING");
        break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
         Log.i("CALL","CALL_STATE_OFFHOOK");
        break;
        case TelephonyManager.CALL_STATE_IDLE:
         Log.i("CALL","CALL_STATE_IDLE");
        break;
        }
      }
   }
 }

困扰我:

  E/MSimTelephonyManager(621): MSimTelephonyManager sRegistryMsim != null

LogCat调试Samsung ST 7652时,为什么它会在CALL_STATE_IDLE中提供类似的内容,否则会向我显示完整的呼叫状态记录?

尝试

PhoneStateListener not working with some devices。但它并没有帮助我解决方案总是给我null和来电号码{{1}}。

1 个答案:

答案 0 :(得分:3)

在浏览了几个有关PhoneStateListenersandroid.intent.action.PHONE_STATE的消息来源后,发现了我正在寻找的内容,并且无论模型特定的障碍如何,它都像魅力一样。

<强> 广播接收器

 public class PhoneStateReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String str = intent.getAction();
    if ("android.intent.action.PHONE_STATE".equals(str))
        inComing(context, intent);

    if ("android.intent.action.NEW_OUTGOING_CALL".equals(str))
        outGoing(context, intent);
  }

 private void inComing(Context context, Intent intent){
    String callState = intent.getStringExtra("state");
    if ("RINGING".equals(callState)){
        Log.i(TAG, "RINGING SENDS BUSY");
    }else if ("OFFHOOK".equals(callState)){
        Log.i(TAG, "OFFHOOK SENDS BUSY");
    }else if("IDLE".equals(callState)){
        Log.i(TAG, "IDLE SENDS AVAILABLE"); 
    }
  }

 private void trueCallerOutgoing(Context context, Intent intent)
  {
    String callState = intent.getStringExtra("state");
    if ("RINGING".equals(callState)){
            Log.i(TAG, "RINGING SENDS BUSY");
    }else if ("OFFHOOK".equals(callState)){
            Log.i(TAG, "OFFHOOK SENDS BUSY");
    }else if("IDLE".equals(callState)){
            Log.i(TAG, "IDLE SENDS AVAILABLE"); 
    }
  }
}

<强> 清单

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

有权限

  <uses-permission android:name="android.permission.READ_PHONE_STATE" />