即使完成工作,alertdialogbox也不会关闭

时间:2019-07-09 09:34:22

标签: android android-alertdialog

我正在制作一个显示纬度值的应用程序,但我已获得所有代码,但我需要一个onPost恢复方法,该方法将检查GPS是否已启用(如果已启用),如果未启用,则它应该可以工作,否则应保留在除非它启用了GPS才能正常工作,否则即使没有启用GPS,但即使GPS启用,它也会显示alertdialogbox,如果gps启用,则如何编码,它应该不显示alertdialogbox,如果可以的话,请帮帮我。    我有一个方法,但是现在即使在

上启用它后,它的显示对话框
code on on resume is : 



    help me out if i am missing something
@override    
     AlertDialog.Builder alerDialogbuilder = new AlertDialog.Builder(Main3Activity.this);
                alerDialogbuilder.setTitle("Enable Gps to Continue");
                alerDialogbuilder.setMessage("If You Want To Enable Gps Go To Settings");
                alerDialogbuilder.setCancelable(false);
                alerDialogbuilder.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        Intent intent1 = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(intent1);
                        Toast.makeText(getApplicationContext(), "Enable Gps..", Toast.LENGTH_SHORT).show();

                    }
                });


                alerDialogbuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(getApplicationContext(), "Uploading Failed,Enable Gps", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(Main3Activity.this, Main2Activity.class);
                        startActivity(intent);

                    }
                });
                AlertDialog alertDialog = alerDialogbuilder.create();
                alertDialog.show();
            }




Alertdialogbox not Disappearing even when gps is enabled

2 个答案:

答案 0 :(得分:0)

setting class to on resume

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        boolean gps_enabled = isLocationEnabled(context);

        Log.i("OnRe"," gps_enabled: "+ gps_enabled);

        if (!gps_enabled) {
            // notify user
            AlertDialog.Builder alerDialogbuilder = new AlertDialog.Builder(Main3Activity.this);
            alerDialogbuilder.setTitle("Enable Gps to Continue");
            alerDialogbuilder.setMessage("If You Want To Enable Gps Go To Settings");
            alerDialogbuilder.setCancelable(false);
            alerDialogbuilder.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                    Intent intent1 = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(intent1);
                    Toast.makeText(getApplicationContext(), "Enable Gps..", Toast.LENGTH_SHORT).show();

                }
            });


            alerDialogbuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(getApplicationContext(), "Uploading Failed,Enable Gps", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Main3Activity.this, Main2Activity.class);
                    startActivity(intent);

                }
            });
            AlertDialog alertDialog = alerDialogbuilder.create();
            alertDialog.show();
        }


Created a different class
public static Boolean isLocationEnabled(Context context)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// This is new method provided in API 28
            LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            return lm.isLocationEnabled();
        } else {
// This is Deprecated in API 28
            int mode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,
                    Settings.Secure.LOCATION_MODE_OFF);
            return  (mode != Settings.Secure.LOCATION_MODE_OFF);

        }
    }

答案 1 :(得分:0)

进行此更改:

alerDialogbuilder.setCancelable(true);