有没有办法检测呼出电话是否成功接听或接听?我正在使用Intent.ACTION_CALL
拨打电话,并使用PhoneCallListener
在接听呼出电话时查找呼叫的状态,但我无法做到这一点。这在android中是可能的吗?
发布于 2016-04-26 16:01:03
在深入研究这个问题后,我得出了这样的结论:
PhoneStateListener
对去电不起作用,它会调用OFFHOOK
而不是RINGING
,而且OFFHOOK
永远不会在应答时被调用。NotificationListenerService
,你可以收听与去电相关的发布通知。您可以执行类似以下代码的操作。这里的问题是,我是不能从一些三星手机的通知文本,而且文本本身可能会改变很多,从一部手机到另一部。还需要API18及更高版本的。公共类NotificationListener扩展NotificationListenerService {私有字符串TAG = this.getClass().getSimpleName();@Override public void onNotificationPosted(StatusBarNotification sbn) { Log.i(TAG,“通知已发布”);Log.i(TAG,sbn.getPackageName() + "\t“+ sbn.getNotification().tickerText + "\t”+Log.iBundle extras = sbn.getNotification().extras;if (“正在进行的新意图{startService(sbn.getNotification,.extras} 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
更基础,我觉得所有接口都支持。但也使用AccessibilityService,一些手机不会在call Answer情况下发布有用的事件。在大多数手机中,一旦呼叫应答,就会引发一个事件,并带有呼叫持续时间;其打印输出如下所示: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;
STATE_ACTIVE
。你可以通过API23,Marshmallow.,用你自己的UI替换手机的默认InCallUI,我还没试过用这个,但不管怎样,它只限于InCallService
总而言之,您需要构建一个结合NotificationListener
和AccessibilityService
的解决方案,以便覆盖所有手机,希望如此。
发布于 2014-10-05 06:17:02
您需要结合使用RINGING
状态和OFFHOOK
状态来确定呼叫是否被应答。
有关如何实现这一点的实际代码,请参阅此thread。
发布于 2018-03-24 17:41:19
我知道这已经有一段时间了,但我希望对仍在寻找解决方案的人有所帮助!
我最近不得不在一个类似的项目中工作,我需要捕获呼出呼叫的振铃状态,而我唯一能找到的方法就是使用本地拨号应用程序的隐藏Api。由于api的变化,这只有android 5.0以上版本才有可能。这在Android 5.0.1上进行了测试,效果很好。(附注:你需要一个有根的设备才能让它工作,因为你需要将你的应用程序安装为系统应用程序(google how!)其然后将能够检测呼出呼叫状态)。
根据记录,PhoneStateListener不能检测许多帖子中提到的呼出状态。
首先,在清单文件中添加此权限。
<uses-permission android:name="android.permission.READ_PRECISE_PHONE_STATE" />
然后定义您的broadcastreceiver,(这里是示例代码!)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
public class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, final Intent intent)
{
switch (intent.getIntExtra("foreground_state", -2)) {
case 0: // PreciseCallState.PRECISE_CALL_STATE_IDLE:
System.out.println("IDLE");
break;
case 3: // PreciseCallState.PRECISE_CALL_STATE_DIALING:
System.out.println("DIALING");
break;
case 4: // PreciseCallState.PRECISE_CALL_STATE_ALERTING:
System.out.println("ALERTING");
break;
case 1: // PreciseCallState.PRECISE_CALL_STATE_ACTIVE:
System.out.println("ACTIVE");
break;
}
}
}
我用常量的值替换了一些常量,因为我在不熟悉反射概念的人中看到了很多困惑(为了简单起见)。警报基本上是接收器实际响铃时的状态!这还不包括呼叫建立时间,这意味着呼叫已被成功接收!在precise_call_state类中还有其他选项(我没有探究),可以帮助您捕获被应答的呼叫!
https://stackoverflow.com/questions/26190318
复制相似问题