Android 当我收到广播时是否显示AlertDialog?在SDK中>;=23

Android 当我收到广播时是否显示AlertDialog?在SDK中>;=23,android,broadcastreceiver,android-6.0-marshmallow,Android,Broadcastreceiver,Android 6.0 Marshmallow,我想这样做:我可以使用LoginActivity登录并进入MainActivity,MainActivity有一个按钮,当我点击按钮发送广播时,activity将显示一个警报对话框,当我点击确定,它将完成所有活动并返回到LoginActivity 我已经设置了alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT),并在manifest.xml中写入,当SDK=23时如何运行它?或者我应该如何从代码

我想这样做:我可以使用
LoginActivity
登录并进入
MainActivity
MainActivity
有一个
按钮
,当我点击
按钮
发送广播时,activity将显示一个
警报对话框
,当我点击
确定
,它将完成所有活动并返回到
LoginActivity

我已经设置了
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)
,并在manifest.xml中写入
,当SDK<23时它可以运行。但是当SDK>=23时如何运行它?或者我应该如何从代码中执行

@Override
    public void onReceive(final Context context, Intent intent) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setTitle("Warning");
        dialogBuilder.setMessage("You are forced to be offline. Please try to login again.");
        dialogBuilder.setCancelable(false);
        dialogBuilder.setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (Build.VERSION.SDK_INT >= 23){
                            if (Settings.canDrawOverlays(context)){
                                ActivityCollector.finishAll();
                                Intent intent = new Intent(context, LoginActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                context.startActivity(intent);
                            } else {
                                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                                context.startActivity(intent);
                            }
                        } else {
                            ActivityCollector.finishAll();
                            Intent intent = new Intent(context, LoginActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(intent);
                        }

                    }
                });
        AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alertDialog.show();
    }
试试这个:

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 23){

   //Do what you want

} else{

}
试试这个:

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 23){

   //Do what you want

} else{

}

最好使用活动作为对话。对不起,我是安卓beginer,你能描述清楚吗?恐惧。使用此示例创建一个Actvity,并启动此活动作为显示对话框的位置。最好使用活动作为对话框。对不起,我是android beginer,您能清楚地描述它吗?恐惧。用这个例子创建一个activity,并开始这个活动,作为显示对话框的地方。我想我已经写了你说的那样,你能更清楚地帮助我吗?谢谢:DIf api版本大于23意味着你必须显示dialog konw。所以在使用该代码的api版本之前检查。它将返回你的设备正在使用的api版本。你明白吗?是的,我知道,但我已经在我编写的代码中完成了。我不能随便使用它,你能帮我在我的代码中解决它吗。thx:我想我已经像你说的那样写了,你能帮我更清楚吗?谢谢:DIf api版本大于23意味着你必须显示dialog konw。所以在使用该代码的api版本之前检查一下。它会返回你的设备正在使用的api版本。你明白吗?是的,我知道,但我已经在我的代码中完成了,这是我写的。我不能粗暴地使用它,你能帮我在代码中解决它吗