gpt4 book ai didi

安卓.view.WindowManager$BadTokenException : Unable to add window while showing a AlertDialog from broadcast receiver

转载 作者:行者123 更新时间:2023-11-30 00:02:49 27 4
gpt4 key购买 nike

我已经设置了警报管理器我想在 BroadcastReceiver 的 onReceive 方法中显示一个对话框。

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Utils.getINSTANCE().statusCheck(context);
}
}

这里是statuscheck方法

    public void statusCheck(Context context) {
final LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps(context);

}
}
private void buildAlertMessageNoGps(final Context context) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
context. startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}

我在 alert.show() 上遇到问题 token null

java.lang.RuntimeException: Unable to start receiver com.coin.etheriumclassic.AlarmReceiver: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

我们该怎么做?如果我们必须在 Broadcastreceiver 中显示简单的对话框(我在 broadcastreceiver 中这样做是因为我必须在设备打开时显示对话)

最佳答案

您只能显示来自 ActivityAlertDialog,而不是 BroadcastReceiver

可以BroadcastReceiver 启动一个 Activity ,因此您可以创建一个带有对话框主题(例如 Theme.Material.Dialog )的 Activity ,所以它的样式如下一个对话框。

但请注意,在设备启动时显示对话框对用户来说相当不利,更不用说不切实际了(例如,设备有锁屏)。考虑改为显示 Notification

关于安卓.view.WindowManager$BadTokenException : Unable to add window while showing a AlertDialog from broadcast receiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49592218/

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