gpt4 book ai didi

android - 检测用户是否实际以编程方式调用了号码

转载 作者:太空狗 更新时间:2023-10-29 13:52:38 24 4
gpt4 key购买 nike

在一个应用程序中,我在 TextView 中显示了一些电话号码,用户可以点击它们来调用它们(autoLink 在 TextView 上设置为 phone)。
当我点击数字时,会出现调用该号码的选项。
我的问题是:有没有办法确定用户是否实际按下了拨号盘中的拨号按钮来实际调用电话?

最佳答案

您可以创建一个 GSMBroadcastListener 并接收有关电话调用状态的事件(挂断、正在响铃、已建立等)。

如果你想知道,如果它发生在点击你的电话按钮之后,只需在点击事件中创建这个监听器,这样它只会在你的一个按钮被点击时接收事件。通话结束后在Hangup-Event中Unhook(注销),所以不会收到通话后的事件。

这是我在生产应用程序中使用监听器接口(interface)的实现:

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

代码:

public interface GSMBroadcastListener {
void onHangUp(
void onEstablished();
void onRinging();
}


public class GSMBroadcastReceiver extends BroadcastReceiver {

private static GSMBroadcastListener handler = null;
private static PrivateListener privateListener = null;

public static void registerGSMBroadcastListener(@Nullable GSMBroadcastListener listener) {
handler = listener;
}

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

if (privateListener == null) {
privateListener = new PrivateListener();
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(privateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

}

private class PrivateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);

Log.i("PHONE_STATE", String.format("GSM event \"onCallStateChanged\" received: state=%d; incomingNumber=\"%s\";", state, incomingNumber));
if (handler != null) {
if (state == TelephonyManager.CALL_STATE_IDLE) {
Log.i("PHONE_STATE", "Forwarding event as \"GSM onHangUp\".");
handler.onHangUp();
} else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
Log.i("PHONE_STATE", "Forwarding event as \"GSM onEstablished\".");
handler.onEstablished();
} else if (state == TelephonyManager.CALL_STATE_RINGING) {
Log.i("PHONE_STATE", "Forwarding event as \"GSM onRinging\".");
handler.onRinging();
}
}
}
}

}

此类会将 GSM 事件转发给您使用 GSMBroadcastReceiver.registerGSMBroadcastListener 添加的监听器。

要使用它,您需要:

  • 在您的 list 中注册接收器

    <receiver android:name=".handlers.GSMBroadcastReceiver">
    <intent-filter>
    <action android:name="android.intent.action.PHONE_STATE"/>
    </intent-filter>
    </receiver>
  • 然后,在您的电话号码按钮的点击监听器中,注册一个监听器:(注意,您在 onHangUp() 回调中注销了它!)

    GSMBroadcastReceiver.registerGSMBroadcastListener(new GSMBroadcastListener() {
    @Override
    public void onRinging() {
    Log.i("GSM", "GSM event \"onRinging\" received.");
    }

    @Override
    public void onEstablished() {
    Log.i("GSM", "GSM event \"onEstablished\" received.");
    }

    @Override
    public void onHangUp() {
    Log.i("GSM", "GSM event \"onHangUp\" received.");
    GSMBroadcastReceiver.registerGSMBroadcastListener(null); // Unregister the handler!
    }
    });

就是这样!单击按钮后,您现在应该会收到有关 GSM Activity 的通知。

希望对您有所帮助,干杯,灰色

关于android - 检测用户是否实际以编程方式调用了号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44284134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com