代码之家  ›  专栏  ›  技术社区  ›  Nostromo

有人打电话时收到通知

  •  0
  • Nostromo  · 技术社区  · 7 年前

    如果我理解正确的话,第一个包含Android特有的代码,第二个包含可以在Android、iOS或Windows Phone中使用的代码。

    我的解决方案在Android仿真的调试模式下运行,现在我想在有来电的时候得到通知,我想得到打电话给我的电话号码。

    Google告诉我,我需要创建一个继承自 BroadcastReceiver 并覆盖 OnReceive

    我找不到关于这门课该怎么办的任何信息?在哪里实例化它?在我的“AndroidApp1”项目中,我如何得到通知和电话号码以作出反应?

    这是我的源代码 (摘自互联网):

    [BroadcastReceiver(Enabled = true, Exported = false)]
    [IntentFilter(new[] { "android.intent.action.PHONE_STATE" })]
    public class IncomingCallReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            // ensure there is information
            if (intent.Extras != null)
            {
                // get the incoming call state
                string state = intent.GetStringExtra(TelephonyManager.ExtraState);
    
                // check the current state
                if (state == TelephonyManager.ExtraStateRinging)
                {
                    // read the incoming call telephone number...
                    string telephone = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);
                    // check the reade telephone
                    if (string.IsNullOrEmpty(telephone))
                        telephone = string.Empty;
                }
                else if (state == TelephonyManager.ExtraStateOffhook)
                {
                    // incoming call answer
                }
                else if (state == TelephonyManager.ExtraStateIdle)
                {
                    // incoming call end
                }
            }
        }
    }
    
    3 回复  |  直到 7 年前
        1
  •  0
  •   Ramesh Yankati    7 年前

    您需要为呼叫状态注册brodcast接收器。

      <action android:name="android.intent.action.PHONE_STATE" />
      <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    

    并在获取广播事件后创建通知。

    确保已定义权限。

     <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    
        2
  •  0
  •   Ramesh Yankati    7 年前

            /**
             * Listener to detect incoming calls. 
             */
            private class CallStateListener extends PhoneStateListener {
               @Override
               public void onCallStateChanged(int state, String incomingNumber) {
                 switch (state) {
                   case TelephonyManager.CALL_STATE_RINGING:
                   // called when someone is ringing to this phone
    
                 Toast.makeText(ctx, "Incoming: "+incomingNumber, 
                   Toast.LENGTH_LONG).show();
                   break;
    
               }
             }
    
    
          You can register the lister for incoming call events.
          tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
          tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    
         Here is the more info 
         https://stackoverflow.com/questions/9684866/how-to-detect-when-phone-is-answered-or-rejected
    
        3
  •  0
  •   Nostromo    7 年前

    我终于向前迈进了一步。我创建了一个类 StateListener 继承自 PhoneStateListener

    public class StateListener : PhoneStateListener
    {
        public override void OnCallStateChanged(CallState state, string incomingNumber)
        {
            base.OnCallStateChanged(state, incomingNumber);
    
            switch (state)
            {
                case CallState.Ringing:
                    break;
                case CallState.Offhook:
                    break;
                case CallState.Idle:
                    break;
            }
        }
    }
    

    然后我在 OnCreate MainActivity 使用以下三行代码创建Android特定项目的类:

    StateListener phoneStateListener = new StateListener();
    TelephonyManager telephonyManager = (TelephonyManager)GetSystemService(Context.TelephonyService);
    telephonyManager.Listen(phoneStateListener, PhoneStateListenerFlags.CallState);
    

    现在当我在 case switch (state) OnCallStateChanged 它们断了,但是 incomingNumber

    所以,这将是我的下一步,得到呼叫号码。

    推荐文章