此方法可以解决部分手机需要在设置中开启 允许显示悬浮窗
在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的值
参考: