Application中弹出Dialog

在日常的开发过程中,经常需要在全局范围内,控制一些信息。比如说,网络状况。我们首先想到的当然是在Application中,进行广播的监听,但是由于Dialog中Context需要的是个Activity,让我们总也不能够得逞。
最近发现了在Applcation中,可以实现弹出Dialog。


            AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
            builder.setMessage("你想恢复下载 ?").setCancelable(false).setPositiveButton("删除", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }
            }).setNeutralButton("恢复", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.setCancelable(false);
            ***alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);***
            alert.show();

注意看清楚倒数第二行代码,然后在Manifest中添加一个,

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

即可。

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android应用中弹全局对话框,你可以使用系统提供的WindowManager来实现。下面是一个简单的示例代码: ```java public class GlobalDialog { private WindowManager windowManager; private Context context; private View dialogView; public GlobalDialog(Context context) { this.context = context; } public void showDialog() { // 初始化对话框布局 dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_layout, null); // 设置对话框的宽高 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; // 设置窗口类型为全局对话框 layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; // 获取WindowManager实例 windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); // 将对话框添加到WindowManager中 windowManager.addView(dialogView, layoutParams); } public void dismissDialog() { if (windowManager != null && dialogView != null) { windowManager.removeView(dialogView); windowManager = null; dialogView = null; } } } ``` 在上述代码中,我们创建了一个`GlobalDialog`类来管理全局对话框。通过调用`showDialog()`方法,我们可以在应用中弹全局对话框,而调用`dismissDialog()`方法则可以关闭对话框。 在布局文件`dialog_layout.xml`中,你可以自定义对话框的样式和内容。 请注意,要使用全局对话框,你的应用必须拥有`SYSTEM_ALERT_WINDOW`权限。你可以在AndroidManifest.xml文件中添加以下权限声明: ```xml <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> ``` 同时,从Android 6.0(API级别23)开始,你还需要在运行时请求该权限。 希望这个例子对你有所帮助!如有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值