【问题标题】:PhoneStateListener for call receiver not working in real device呼叫接收器的 PhoneStateListener 在真实设备中不起作用
【发布时间】:2014-07-16 05:49:38
【问题描述】:

我创建了BroadCastReceiver,它监听IncomingCallOutgoing Calls。尝试在BroadCastReceiver 中添加PhoneStateListenerTelephonyManager,这在模拟器和某些设备上完美运行。

它不适用于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

为什么当我为 Samsung ST 7652 调试时它会在 LogCat 中给出类似的内容,否则它会显示完美的呼叫状态日志?

试过

PhoneStateListener not working with some devices。但它也没有帮助我解决这个问题总是给我CALL_STATE_IDLE和传入号码null

【问题讨论】:

    标签: android broadcastreceiver telephonymanager phone-state-listener


    【解决方案1】:

    在浏览了有关 PhoneStateListenersandroid.intent.action.PHONE_STATE 的多个来源后,我找到了我正在寻找的东西,并且无论模型特定的障碍如何,它都能像魅力一样工作。

    BroadCastReceiver

     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" />
    

    【讨论】:

    • 这不起作用。只调用空闲和摘机。并且在实际接受呼叫时没有监听器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多