[英]TelephonyManager deprecated listen() CALL_STATE_RINGING on android 12
I'd like to listen if there's a phone call happening while my app is in the foreground.当我的应用程序在前台时,我想听听是否有电话发生。
It was like this before but now listen() is deprecated:以前是这样,但现在 listen() 已弃用:
val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
tm.listen(object : PhoneStateListener() {
override fun onCallStateChanged(state: Int, phoneNumber: String?) {
super.onCallStateChanged(state, phoneNumber)
when (state) {
TelephonyManager.CALL_STATE_RINGING -> transcribingAudioConsumer.stopTranscription(null)
else -> {}
}
}
}, PhoneStateListener.LISTEN_CALL_STATE)
I tried something like this but I couldn't find the correct way to implement it.我尝试过这样的事情,但我找不到实现它的正确方法。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
tm.registerTelephonyCallback({ it.run() }, MyPhoneStateListener())
}
@RequiresApi(Build.VERSION_CODES.S)
class MyPhoneStateListener : TelephonyCallback(), TelephonyCallback.CallStateListener {
override fun onCallStateChanged(state: Int) {
when (state) {
TelephonyManager.CALL_STATE_RINGING -> {
Timber.e("omg RING")
}
TelephonyManager.CALL_STATE_OFFHOOK -> {
Timber.e("omg hook")
}
TelephonyManager.CALL_STATE_IDLE -> {
Timber.e("omg idle")
}
}
}
}
I used what you did and android 12 emulator also worked.I used this for versions less than android 12 I hope it works.我使用了您所做的,并且 android 12 模拟器也可以工作。我将它用于低于 android 12 的版本,我希望它可以工作。
class MyPhoneStateListener() : PhoneStateListener() {
override fun onCallStateChanged(state: Int, incomingNumber: String) {
when (state) {
TelephonyManager.CALL_STATE_IDLE -> {
Log.e("MyPhoneStateListener", "IDLE")
}
TelephonyManager.CALL_STATE_OFFHOOK -> {
Log.e("MyPhoneStateListener", "OFFHOOK")
}
TelephonyManager.CALL_STATE_RINGING -> {
Log.e("MyPhoneStateListener", "RINGING")
}
}
}
}
I used this to listen to the call我用这个来听电话
phoneListener = MyPhoneStateListener()
var telephony: TelephonyManager? = null
telephony = context
.getSystemService(TELEPHONY_SERVICE) as TelephonyManager?
telephony!!.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE)
finally you should make a ServiceReceiver class like here.最后你应该像这里一样创建一个 ServiceReceiver 类。 Add PhoneStateListener
添加 PhoneStateListener
ServiceReceiver(Java)服务接收器(Java)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.telephony.TelephonyCallback;
import android.telephony.TelephonyManager;
public class ServiceReceiver extends BroadcastReceiver {
@RequiresApi(api = Build.VERSION_CODES.S)
@Override
public void onReceive(Context context, Intent intent) {
registerCustomTelephonyCallback(context);
}
@RequiresApi(Build.VERSION_CODES.S)
class CustomTelephonyCallback extends TelephonyCallback implements TelephonyCallback.CallStateListener {
private CallBack mCallBack;
public CustomTelephonyCallback(CallBack callBack) {
mCallBack = callBack;
}
@Override
public void onCallStateChanged(int state) {
mCallBack.callStateChanged(state);
}
}
@RequiresApi(Build.VERSION_CODES.S)
public void registerCustomTelephonyCallback(Context context) {
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.registerTelephonyCallback(context.getMainExecutor(), new CustomTelephonyCallback(new CallBack() {
@Override
public void callStateChanged(int state) {
int myState=state;
}
}));
}
interface CallBack {
void callStateChanged(int state);
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.