【问题标题】:What should I choose between BroadcastReceiver and PhoneStateListener when I need to detect call event in android?当我需要在android中检测呼叫事件时,我应该在BroadcastReceiver和PhoneStateListener之间选择什么?
【发布时间】:2014-04-14 10:13:51
【问题描述】:

我已阅读Detecting outgoing call and call hangup event in android

我想做一个应用程序来检测后台呼叫事件作为服务,我想我应该选择BroadcastReceiver,因为即使我退出应用程序,应用程序也会继续检测呼叫。我认为停止检测呼叫的唯一方法是在我使用 BroadcastReceiver 时卸载应用程序。

如果我选择PhoneStateListener,我认为如果我退出应用程序将停止检测呼叫。

对吗?

谢谢!

【问题讨论】:

    标签: android


    【解决方案1】:

    在我看来,最好的方法是使用最简单的解决方案。也就是说,PhoneStateListener 会做你想做的事——在后台检测呼叫事件:

    public class IncomingCallReciever extends BroadcastReceiver {
    
        private Context mContext;
        private Intent mIntent;
    
        @Override
        public void onReceive(Context context, Intent intent) {
            mContext = context;
            mIntent = intent;
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            int events = PhoneStateListener.LISTEN_CALL_STATE;
            tm.listen(phoneStateListener, events);
        }
    
        private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                String callState = "UNKNOWN";
                switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    callState = "IDLE";
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    // -- check international call or not.
                    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:
                    String dialingNumber = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                    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 + ")";
                    }
                    break;
                }
                Log.i(">>>Broadcast", "onCallStateChanged " + callState);
                super.onCallStateChanged(state, incomingNumber);
            }
        };
    
    }
    

    要访问状态,您需要在manifest file 中声明权限:

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

    PhoneStateListener.LISTEN_CALL_STATE 所做的事情令人惊叹,实际上它比乍一看要容易得多,因为它为您提供了监控呼叫事件所需的一切。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多