简体   繁体   English

Android - 如何检测用户是否拒绝了来电?

[英]Android - How to detect if user rejected an incoming call?

I'm making an Android app with incoming calls.我正在制作一个带有来电的 Android 应用程序。 Is there any way in my app, to know if user rejected any incoming call before answering it?我的应用程序中有什么方法可以知道用户在接听来电之前是否拒绝了任何来电?

First, you have to add a permission in manifest file.首先,您必须在清单文件中添加权限。

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Then you have to register a broadcast receiver in the manifest file.然后你必须在清单文件中注册一个广播接收器。

<receiver android:name=".YourReceiver">
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

Then write in your broadcast receiver's onReceive method:然后写入广播接收器的 onReceive 方法:

public void onReceive(Context context, Intent intent) {
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 
    if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
        //If the phone is **Ringing**
    }else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK){
        //If the call is **Received**
    }else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
        //If the call is **Dropped** or **Rejected**
    }
}

If the state changes from TelephonyManager.EXTRA_STATE_RINGING to TelephonyManager.EXTRA_STATE_IDLE , then it will be a missed call.如果状态从TelephonyManager.EXTRA_STATE_RINGING更改为TelephonyManager.EXTRA_STATE_IDLE ,那么它将是未接来电。

Like this you have to check the conditions.像这样你必须检查条件。

Please put a log in those conditions.请记录这些条件。 Then see the logs.然后查看日志。 I think you will get your answer.我想你会得到你的答案。

It's been a while since you asked, but I think it might be useful for future reference for others.自从你问起已经有一段时间了,但我认为它可能对其他人未来的参考有用。

If you extract data from Android's calllog (eg to XML or in your case to a variable in your app) you have two fields of interest:如果您从 Android 的 calllog 中提取数据(例如到 XML 或在您的应用程序中的变量),您有两个感兴趣的字段:

1) numbertype eg 1 for Incoming, 2 for Outgoing, 3 for missed 1)数字类型,例如 1 表示传入,2 表示传出,3 表示未接

2) duration (in seconds) 2)持续时间(以秒为单位)

Rejected number is (as you correctly mentioned) treated as numbertype=1.拒绝的数字(如您正确提到的)被视为 numbertype=1。 However if you combine numbertype=1 AND duration=0 (since any call answered will have duration>0) then this hopefully solves your problem.但是,如果您结合使用 numbertype=1 AND duration=0(因为接听的任何电话的持续时间>0),那么这有望解决您的问题。

I'm using Android 6, not sure if the types changed since then, but with me the above method works 99.9% of the time.我使用的是 Android 6,不确定从那以后类型是否发生了变化,但对我来说,上述方法在 99.9% 的时间里都有效。 Never managed to hang up the phone in less than a second :)从未设法在不到一秒钟的时间内挂断电话:)

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

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