在android中显示来自非活动类的对话框警报

编程入门 行业动态 更新时间:2024-10-21 06:40:12
本文介绍了在android中显示来自非活动类的对话框警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I want to show an Alert Dialog via AlertDialogManager class to a non-activity class DeviceAdminReceiverSample's method onDisabled, but whenever I call alertDialog via that method it generates error with following text

错误

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() 但都是徒劳的.我的两个类的代码如下

I know the issue is with context thing but I don't know what to put there so that it work, I tried this, getApplicationContext() but all vain. My code for both classes is below

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); }

推荐答案

问题是您只能显示来自活动的 AlertDialogs".这不是上下文问题.

The problem is 'You can show AlertDialogs from Activity only'. This is not an issue of context.

虽然这不是一个好主意,显示来自接收者的对话(最好使用通知),但如果你想这样做,你可以创建一个活动作为对话和显示

更多推荐

在android中显示来自非活动类的对话框警报

本文发布于:2023-11-26 18:06:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634620.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   对话框   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!