Service中创建Dialog对话框

Android Service中创建Dialog对话框

一般我们都是在Activity中创建Dialog的,无论是系统的AlertDialog还是我们自定义的Dialog。
我们只需设置title,message等信息,然后show出来即可

可是在Service中呢,没有Activity,Dialog没有了依赖,那么怎么办呢? 这时,Dialog就要以系统对话框的形式弹出了。在我们取得Dialog对象后,需给它设置类型,即

mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

同时,必须提供系统弹框对应的权限:

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

注:小米手机等高度自定义的手机,可能还需要手动设置显示悬浮窗的权限。。。(恶心人。。。)

看看官方解释吧:

/**
 * Window type: system window, such as low power alert. These windows
 * are always on top of application windows.
 * In multiuser systems shows only on the owning user's window.
 */
public static final int TYPE_SYSTEM_ALERT       = FIRST_SYSTEM_WINDOW+3;

随手一段原始代码来证明自己吧。。。

   private void createDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("title")
                .setMessage("content")
                .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        Dialog dialog = builder.create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        dialog.show();
    }

完…


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值