英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论