gpt4 book ai didi

android - 在 android 中显示来自非 Activity 类的对话框警报

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:24 24 4
gpt4 key购买 nike

我想通过 AlertDialogManager 显示一个警告对话框类到 non-activityDeviceAdminReceiverSample的方法 onDisabled , 但每当我调用 alertDialog通过该方法,它会生成带有以下文本的错误

错误

06-12 12:01:19.923: E/AndroidRuntime(468): FATAL EXCEPTION: main
06-12 12:01:19.923: E/AndroidRuntime(468): java.lang.RuntimeException: Unable to start
receiver com.android.remotewipedata.DeviceAdminReceiverSample:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not
for an application

我知道问题出在 context 上东西,但我不知道该放什么让它起作用,我试过了 this , getApplicationContext()但都是徒劳的。我对这两个类的代码如下

AlertDialogManager

public class AlertDialogManager {

public void showAlertDialog(Context context, String title, String message,
Boolean status) {
final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);

if (status != null)
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog.show();
}

DeviceAdminReceiverSample

public class DeviceAdminReceiverSample extends DeviceAdminReceiver {
static final String TAG = "DeviceAdminReceiver";
AlertDialogManager alert = new AlertDialogManager();

/** Called when this application is no longer the device administrator. */
@Override
public void onDisabled(Context context, Intent intent) {
super.onDisabled(context, intent);
Toast.makeText(context, R.string.device_admin_disabled,
Toast.LENGTH_LONG).show();
// intent.putExtra("dialogMessage", "Device admin has been disabled");
// intent.setClass(context, DialogActivity.class);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(intent);
alert.showAlertDialog(context, "Alert",
"Device admin has been disabled", true);
}

最佳答案

只需在您的alertDialog.show();

之前添加
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

如果上述方法无效,请尝试以下操作:

alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL); 

并使用此权限:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

关于android - 在 android 中显示来自非 Activity 类的对话框警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17059545/

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