了解用户何时在打电话

发布于 2024-09-27 19:03:49 字数 160 浏览 3 评论 0原文

(Android) 前几天,我看到了一种注册广播接收器的方法,以了解屏幕何时变黑或反转。

现在我想找到一种方法来了解用户何时在打电话,但我没有找到这种情况的广播,那么我如何了解用户是否在打电话? (可能不需要总是运行会使电池放电的服务)。

TNK

瓦莱里奥

(Android) Some days ago I saw a way to register a broadcast receiver to understand when the screen goes black or the inverse.

Now i'd like to find a way to understand when the users is speaking at the phone but i don't find a broadcast for this case, so how can i understand if the user is at the phone? (Possibly without always running a service that would discharge the battery).

Tnk's

Valerio

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七分※倦醒 2024-10-04 19:03:49

我不确定是否在电话上“说话”,因为没有 API 可以检测到这一点,但您可以监听电话状态,例如:

TelephonyManager telephonyManager = (TelephonyManager)
                                       getSystemService(Context.TELEPHONY_SERVICE);

PhoneStateListener listener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        String stateString = "N/A";
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                stateString = "Idle";
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                stateString = "Off Hook";
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                stateString = "Ringing";
                break;
        }
    }
};
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

不要忘记清单中的以下权限:

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

I'm not sure about "speaking" at the phone, since there is no API to detect that, but you can listen to the phone state, like:

TelephonyManager telephonyManager = (TelephonyManager)
                                       getSystemService(Context.TELEPHONY_SERVICE);

PhoneStateListener listener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        String stateString = "N/A";
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                stateString = "Idle";
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                stateString = "Off Hook";
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                stateString = "Ringing";
                break;
        }
    }
};
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

Don't forget the following permission in the manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文