gpt4 book ai didi

java - 关闭应用程序时广播接收器不工作

转载 作者:行者123 更新时间:2023-12-01 22:42:45 27 4
gpt4 key购买 nike

当屏幕上弹出来电时。应用程序在打开时正在运行。但是,当我完全关闭应用程序时,即使有语音通话,手机也不会显示弹出窗口。应用程序关闭后广播接收器不起作用。

我的设备是小米8

这是我的代码:

public class CallBarring extends BroadcastReceiver {

private CustomDialog dialog;
private TelephonyManager telephonyManager;
private PhoneStateListener listener;
private Context context;
private Button btnEndCall;
private TextView incomingNumberName;
private List<String> data;

final NumberInfo numberInfo = new NumberInfo();
Retrofit retrofit = NetworkClient.getRetrofitClient();
NumberAPI numberAPI = retrofit.create(NumberAPI.class);
@Override
public void onReceive(Context context, Intent intent) {

if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
return;

else {
this.context = context;
if(dialog == null){
dialog = new CustomDialog(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}else{
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
dialog.show();
}
// Fetch the number of incoming call
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
listener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
String stateString = "N/A";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
stateString = "Idle";
dialog.dismiss();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stateString = "Off Hook";
dialog.dismiss();
break;
case TelephonyManager.CALL_STATE_RINGING:
stateString = "Ringing";
dialog.show();
break;
}
}
};

telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
telephonyManager.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
System.out.println("incomingNumber : "+incomingNumber);
fetchUserInfo(incomingNumber);


}
},PhoneStateListener.LISTEN_CALL_STATE);

}
}

最佳答案

许多 Android 设备都有电源管理器和剂量模式概念。因此,当应用程序处于后台时,后台功能会停止。

您可以使用前台服务注册BroadcastReceiver来监听PHONE_STATE。

关于java - 关闭应用程序时广播接收器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58486282/

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