简体   繁体   English

Android,处理来电事件

[英]Android, Handling incoming call event

I am developing an application which has a countdown timer. 我正在开发一个具有倒数计时器的应用程序。 I want to pause that timer only if there is an incoming call on the phone. 我只想在电话上有来电时才暂停计时器。 Is there any way to fire an event whenever we receive a call? 每当我们接到电话时,有什么方法可以触发事件?

I think you should extends PhoneStateListener class.In that class you handle the phone state.For that use the permission in the manifest file for handling phone state (ie <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> ). 我认为您应该扩展PhoneStateListener类。在该类中您可以处理电话状态。为此,请使用清单文件中的权限来处理电话状态(即<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> )。

And use TelephonyManager to get the status of phone. 并使用TelephonyManager来获取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.

And override this method. 并重写此方法。

     @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;
    }
}

When a Phone-Call is received OS fires a Message, technically called Broadcast. 收到电话呼叫后,操作系统会触发一条消息,技术上称为广播。

Any Application can view/Respond to this message via Registering for PhoneIntentReceiver, if more than one Application installed has registered for this, then all of them are given a chance to view this message based on Priority. 任何应用程序都可以通过注册PhoneIntentReceiver来查看/响应此消息,如果已为此安装了多个应用程序,则所有这些应用程序都将有机会根据优先级查看此消息。

You can register for PhoneIntentReceiver via Manifest or Programatically. 您可以通过清单或以编程方式注册PhoneIntentReceiver。 In both case you specify a Class that extends broadcast receiver in your project, that will receive a callback on detecting an Incoming call. 在这两种情况下,您都可以指定一个扩展项目中广播接收器的类,该类将在检测到传入呼叫时收到回调。

Then in this class the control is passed on to onReceive method. 然后,在此类中,控件将传递给onReceive方法。 Its here that you can Stop your Timmer. 在这里,您可以停止调音。

This is the story behind it.Happy Coding. 这就是背后的故事。快乐编码。

You have to write the Broadcast receiver which listens for incoming call 您必须编写广播接收器来监听传入的呼叫

see this link for more info... 查看链接以获取更多信息...

you have to use broadcast receiver for that........ 为此,您必须使用广播接收器。

First of all register your receiver in manifest.xml 首先在manifest.xml中注册接收者

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

then you have to handle that reciever in 那么你必须在

public class PhoneIntentReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) { } }

I would say the best implementation would be to utilize time stamps, timers (java.util.Timer or android.app.AlarmManager), and then listen for phone events using a broadcast receiver. 我想说最好的实现是利用时间戳,计时器(java.util.Timer或android.app.AlarmManager),然后使用广播接收器监听电话事件。

Basically every time you need to start an alarm for a certain period of time store a timestamp for the start of that alarm(probably easiest in an sql db) and then start the timer/alarm. 基本上,每次需要在特定时间段内启动警报时,都应为该警报的启动存储时间戳(在sql db中最简单),然后启动计时器/警报。 When an alarm goes off make sure to clean up your stored timestamps. 当警报响起时,请确保清理您存储的时间戳。

Make sure to listen to phone state changes and on a phone call answered change clear all your alarms/timers and record the stop dates along with the previous timestamps and then when the phone call ends (from your reciever event) restart the timers/alarms for the remaining time. 确保收听电话状态变化,并在接听电话变化时清除所有警报/计时器,并记录停止日期以及以前的时间戳,然后在电话结束(来自接收者事件)后重新启动计时器/警报剩余时间。

In your Broadcastreceiver onReceive() write following code 在您的Broadcastreceiver onReceive()编写以下代码

Don't forget to give appropriate permission 不要忘记给予适当的许可

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;
        }

    }
}
public class OutgoingCallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

           Toast.makeText(context, "My Toast", Toast.LENGTH_LONG).show();
    }
}

Try This receiver to fire an event 尝试此接收器引发事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM