Android sdk 31 获取通话结束通知。

huangapple go评论66阅读模式
英文:

Android sdk 31 get notified if call is ended

问题

我有一个应用程序,它使用callIntent = new Intent(Intent.ACTION_CALL)来拨打电话。我需要一种方法,在用户结束通话时得到通知。

我曾经使用telephonyManager.listen,但它已经被弃用。我看到我应该使用registerTelephonyCallback,但我不知道如何使用它。

有人可以给我一些关于如何做到这一点的指导吗?

谢谢

英文:

I have an app that uses the callIntent = new Intent(Intent.ACTION_CALL) to make a call. I need a way to get notified when the user ends the call.

I used to use telephonyManager.listen but its been deprecated. I see I am supposed to use registerTelephonyCallback but I cant find out how that works.

Can anyone give me some pointers how to do this

Thanks

答案1

得分: 1

我是这样解决的:

public class MyCallStateListener extends TelephonyCallback implements TelephonyCallback.CallStateListener {
    private String TAG = "MyCallStateListener";
    @Override
    public void onCallStateChanged(int state) {
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE: {
                Log.v(TAG, "状态为CALL_STATE_IDLE");
                break;
            }
            case TelephonyManager.CALL_STATE_OFFHOOK: {
                Log.v(TAG, "状态为CALL_STATE_OFFHOOK");
                break;
            }
            default: {
                Log.v(TAG, "状态为" + state);
            }
        }
    }
}

TelephonyManager telephonyManager =
                (TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE);
        TelephonyCallback telephonyCallback = new MyCallStateListener();
        telephonyManager.registerTelephonyCallback(getContext().getMainExecutor(),  telephonyCallback);
英文:

I solved it as follows

public class MyCallStateListener extends TelephonyCallback implements TelephonyCallback.CallStateListener {
    private String TAG = "MyCallStateListener";
    @Override
    public void onCallStateChanged(int state) {
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE: {
                Log.v(TAG, "&&&&&&&&&&&&&&&&&&&&&&&&&& state is CALL_STATE_IDLE");
                break;
            }
            case TelephonyManager.CALL_STATE_OFFHOOK: {
                Log.v(TAG, "&&&&&&&&&&&&&&&&&&&&&&&&&&& state is CALL_STATE_OFFHOOK");
                break;
            }
            default: {
                Log.v(TAG, "state is " + state);
            }
        }
    }
}

TelephonyManager telephonyManager =
                (TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE);
        TelephonyCallback telephonyCallback = new MyCallStateListener();
        telephonyManager.registerTelephonyCallback(getContext().getMainExecutor(),  telephonyCallback);

huangapple
  • 本文由 发表于 2023年2月14日 02:14:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定