android - 多次调用警报对话框

标签 android android-alertdialog android-lifecycle

每次用户进入应用程序时,都会调用 onResume()。如果用户没有对警告对话框做出响应,那么对话框就会不断累积。我想检查是否已显示对话框。

我在这里找到了很好的答案:Prevent dialog to be opened multiple times when resuming activity在这里 How do I show only one Dialog at a time?但是在我的代码中实现它时总是出现一些错误

警告对话框

private AlertDialog.Builder showGPSDialog(Context con) {

    AlertDialog.Builder builder = new AlertDialog.Builder(con);
    builder.setTitle("Enable GPS");
    builder.setMessage("Please enable GPS");

    builder.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            startActivity(
                    new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        }
    });
    builder.setNegativeButton("Ignore", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    return builder;
}

onResume()

LocationManager mlocManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    boolean isEnabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if(!isEnabled) {
        showGPSDialog(MapsActivity.this).show();
    }

最佳答案

您可以做的是将 boolean isDialogBox 显示在 sharedPreferences 中并使其为真。当用户单击对话框的肯定或否定按钮时。并在 onResume 中检查 isDialogBox 是否为真

把这段代码放在oncreate

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(); 
private AlertDialog.Builder showGPSDialog(Context con) {

    AlertDialog.Builder builder = new AlertDialog.Builder(con);
    builder.setTitle("Enable GPS");
    builder.setMessage("Please enable GPS");

    builder.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        prefs.edit().putBoolean("isShown", true).commit();
            startActivity(
                    new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        }
    });
    builder.setNegativeButton("Ignore", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        prefs.edit().putBoolean("isShown", true).commit()
            dialog.dismiss();
        }
    });
    return builder;
}

这在 onResume 中

LocationManager mlocManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
boolean isEnabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

if(!(prefs.getBoolean("isShown", false))) {
    showGPSDialog(MapsActivity.this).show();
}

关于android - 多次调用警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45662590/

相关文章:

android - 如何从 Android 应用程序的 GridView 中的元素获取值?

android - 在 Activity 之外的 Webview 中显示 AlertDialog

android - 如何防止使用自定义主题的 AlertDialog 全屏显示?

android - 能否画出 Fragment 及其父 FragmentActivity 的生命周期?

android - 如何将我的图像传递到 facebook 提要对话框 android

java - Android 5.0 - 向 RecyclerView 添加页眉/页脚

Android 不在 OnActivityResult 中显示对话框

Android - fragment 中的自定义 View 在 onResume() 中不起作用

android - 在数据绑定(bind)上调用 setLifecycleOwner 时,我应该传递从 getViewLifecycleOwner 返回的 Fragment 实例还是所有者

java - react native 错误 : 'Permission Denial: starting Intent"