BroadcastReceiver中显示对话框showDialog

此方法可以解决部分手机需要在设置中开启 允许显示悬浮窗

onReceive()方法中如下代码:

  @Override
    public void onReceive(final Context context, Intent intent) {
        if (TextUtils.equals(intent.getAction(), AlarmUtils.ALARM_ACTION)) {

            AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.MyAlertDialogStyle);
            builder
                    .setTitle(R.string.app_name)
                    .setMessage(("...."))
                    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setPositiveButton(R.string.watch_right_now, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                           // TODO: 2017/1/22  
                        }
                    });


            builder.setCancelable(false);
            AlertDialog alertDialog = builder.create();
            if (alertDialog.getWindow() != null) {
                int type;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    type = WindowManager.LayoutParams.TYPE_TOAST;
                } else {
                    type = WindowManager.LayoutParams.TYPE_PHONE;
                }
                alertDialog.getWindow().setType(type);
            }
            alertDialog.show();
        }
    }

注意type的值

参考:

  • Android无需权限显示悬浮窗, 兼谈逆向分析app
  • Android 悬浮窗的小结

你可能感兴趣的:(BroadcastReceiver中显示对话框showDialog)