本文介绍了Android-如何检测已接听或接听外拨电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以检测到已成功接听或应答外拨电话?我正在使用Intent.ACTION_CALL拨打电话,并使用PhoneCallListener查找呼出电话时的通话状态,但我一直无法实现.这可能在android中吗?

Is there any way to detect outgoing call is successfully received or answered ? I am using Intent.ACTION_CALL for dialing a call and PhoneCallListener to find the state of a call when outgoing call answered but I couldn't have been achieving this. Is this possible in android ?

推荐答案

在深入研究此问题之后,我得出了以下结论:

After deeply working on this issue, I reached this conclusion:

  1. PhoneStateListener不适用于拨出电话,它会调用OFFHOOK而不是RINGING,并且永远不会在ANSWER上调用OFFHOOK.

  1. PhoneStateListener won't work for outgoing calls, it calls OFFHOOK instead of RINGING and OFFHOOK is never called on ANSWER.

使用NotificationListenerService,您可以收听与去电相关的已发布通知.您可以执行以下代码.这里的问题是我 无法从某些三星手机获取通知文本 ,而且文本本身可能会从一部手机变为另一部手机. 还需要API 18及更高版本 .

Using NotificationListenerService, you can listen to posted notifications related to outgoing calls. You can do something like the below code. The issue here is that I'm not able to get the notification text from some Samsung phones, also the text itself might change a lot from one phone to another. Also it requires API 18 and above.

public class NotificationListener extends NotificationListenerService {

    private String TAG = this.getClass().getSimpleName();

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        Log.i(TAG, "Notification Posted");
        Log.i(TAG, sbn.getPackageName() +
                "\t" + sbn.getNotification().tickerText +
                "\t" + sbn.getNotification().extras.getString(Notification.EXTRA_TEXT);

        Bundle extras = sbn.getNotification().extras;

        if ("Ongoing call".equals(extras.getString(Notification.EXTRA_TEXT))) {
            startService(new Intent(this, ZajilService.class).setAction(ZajilService.ACTION_CALL_ANSWERED));
        } else if ("Dialing".equals(extras.getString(Notification.EXTRA_TEXT))) {
            startService(new Intent(this, ZajilService.class).setAction(ZajilService.ACTION_CALL_DIALING));
        }
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.i(TAG, "********** onNotificationRemoved");
        Log.i(TAG, "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());
    }
}

  • 使用AccessibilityService,它比NotificationListenerService更基本,我认为所有API都支持它.但是,也使用AccessibilityService, 某些电话不会在发生呼叫应答时发布有用的事件 .在大多数电话中,一旦呼叫接听,就会触发一个事件,并带有呼叫持续时间;其打印输出如下:

  • Using AccessibilityService, it is more basic than NotificationListenerService and I think it is supported by all APIs. But also using AccessibilityService, some phones don't publish useful events in case of call Answer. In most phones, an event wil be raised once the call answered, with call duration; Its printout looks like this:

    onAccessibilityEvent EventType: TYPE_WINDOW_CONTENT_CHANGED; EventTime: 21715433; PackageName: com.android.incallui; MovementGranularity: 0; Action: 0 [ ClassName: android.widget.TextView; Text: []; ContentDescription: 0 minutes 0 seconds;

    onAccessibilityEvent EventType: TYPE_WINDOW_CONTENT_CHANGED; EventTime: 21715533; PackageName: com.android.incallui; MovementGranularity: 0; Action: 0 [ ClassName: android.widget.TextView; Text: []; ContentDescription: 0 minutes 1 seconds;

    1. API 23具有一个新类,呼叫.它具有更详细的调用状态; STATE_ACTIVE.您可以通过 InCallService 我还没有尝试使用它,但是无论如何,它只限于API 23(棉花糖).
    1. API 23 has a new class, Call. it has more detailed call states; STATE_ACTIVE. You can replace the default InCallUI of the phone by your own UI through InCallService I didn't try to use this yet, but anyway, it is just limited to API 23, Marshmallow.

    结论是,您需要构建结合了NotificationListenerAccessibilityService的解决方案,以期覆盖所有电话.

    As a conclusion, You need to build a solution combined with NotificationListener and AccessibilityService, in order to cover all phone, hopefully.

    这篇关于Android-如何检测已接听或接听外拨电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!