【问题标题】:Android, Handling incoming call eventAndroid,处理来电事件
【发布时间】:2012-08-14 05:58:27
【问题描述】:

我正在开发一个具有倒数计时器的应用程序。我只想在电话有来电时暂停该计时器。有什么方法可以在我们接到电话时触发事件?

【问题讨论】:

  • 如果用户因为其他事情(例如通知)而暂时离开应用程序,您打算怎么做?
  • @Mikelsrael 我的应用程序需要一个计时器,如果用户只是隐藏活动,它将在后台运行......但是如果只发生任何来电,那么我想暂停计时器
  • 我添加了一个可能的解决方案的大纲,希望它可以有所帮助。

标签: android countdowntimer


【解决方案1】:

我认为您应该扩展PhoneStateListener 类。在该类中您处理电话状态。为此,请使用清单文件中的权限来处理电话状态(即<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">)。

并使用TelephonyManager获取手机状态。

 TelephonyManager  manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
 manager.listen(this, LISTEN_CALL_STATE); // Registers a listener object to receive notification of changes in specified telephony states.

并覆盖此方法。

     @Override
public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);
    switch (state) {
    case TelephonyManager.CALL_STATE_OFFHOOK:
    case TelephonyManager.CALL_STATE_RINGING:
        // Here you can perform your task while phone is ringing.
        break;
    case TelephonyManager.CALL_STATE_IDLE:
        break;
    }
}

【讨论】:

  • 我相信你需要android.permission.READ_PHONE_STATE
【解决方案2】:

当收到电话时,操作系统会触发一条消息,技术上称为广播。

任何应用程序都可以通过注册 PhoneIntentReceiver 查看/响应此消息, 如果安装了多个应用程序已为此注册,则所有这些应用程序都有机会根据优先级查看此消息。

您可以通过 Manifest 或以编程方式注册 PhoneIntentReceiver。在这两种情况下,您都指定了一个扩展项目中广播接收器的类,它将在检测到来电时接收回调。

然后在这个类中,控件被传递给 onReceive 方法。在这里你可以停止你的 Timmer。

这就是它背后的故事。快乐编码。

【讨论】:

    【解决方案3】:

    你必须编写监听来电的广播接收器

    查看this链接了解更多信息...

    【讨论】:

      【解决方案4】:

      你必须为此使用广播接收器............

      首先在 manifest.xml 中注册你的接收器

      <receiver android:name="com.cygnet.phonefinder.receiver.PhoneIntentReceiver" > <intent-filter> <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter> </receiver>

      那么你必须在

      中处理那个接收器
      public class PhoneIntentReceiver extends BroadcastReceiver {
      
      @Override
      public void onReceive(Context context, Intent intent) { } }
      

      【讨论】:

        【解决方案5】:

        我想说最好的实现是利用时间戳、计时器(java.util.Timer 或 android.app.AlarmManager),然后使用广播接收器监听电话事件。

        基本上每次您需要在一段时间内启动警报时,都会存储启动该警报的时间戳(可能在 sql db 中最简单),然后启动计时器/警报。当警报响起时,请务必清理您存储的时间戳。

        确保收听电话状态的变化,并在接听电话时清除所有警报/计时器,并记录停止日期以及之前的时间戳,然后在电话结束时(从您的接收者事件)重新启动计时器/警报剩余时间。

        【讨论】:

          【解决方案6】:

          在您的广播接收器onReceive() 中写入以下代码

          别忘了给予适当的许可

          TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                      MyCallStateListener customPhoneListener = new MyCallStateListener();
          
                      telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
                      if (!intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
                          return;
          
          
          public class MyCallStateListener extends PhoneStateListener {
          
                  public void onCallStateChanged(int state, String incomingNumber) {
          
                  super.onCallStateChanged(state, incomingNumber);
          
                  switch (state) {
                  case TelephonyManager.CALL_STATE_RINGING:
                                  break;
                  case TelephonyManager.CALL_STATE_OFFHOOK:
                                  break;
                  }
          
              }
          }
          

          【讨论】:

            【解决方案7】:
            public class OutgoingCallReceiver extends BroadcastReceiver {
            
                @Override
                public void onReceive(Context context, Intent intent) {
            
                       Toast.makeText(context, "My Toast", Toast.LENGTH_LONG).show();
                }
            }
            

            试试这个接收器来触发一个事件

            【讨论】:

            • 会自动调用这个rec​​eiver吗?没有设置其他任何东西?
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-10-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多