如果活动停止并再次启动,请求启用GPS两次

时间:2017-11-17 09:57:08

标签: android gps

我有一个活动要求用户通过显示带有OK和CANCEL按钮的对话框在onStart()中启用GPS。但是,如果用户在与对话框交互之前按下主页按钮,并再次启动应用程序,则会在旧对话框的顶部显示一个新对话框,因此用户会一个接一个地看到两个对话框。如何关闭活动的onStop方法中的旧对话框?

以下是我的代码的相关部分:

protected void onStart() {
    super.onStart();
    enableGPS();
}

private void enableGPS() {                                      //GPS ENABLE RELATED METHOD
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);
    builder.setAlwaysShow(true);

    PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi
            .checkLocationSettings(mGoogleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            final LocationSettingsStates state = result
                    .getLocationSettingsStates();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:                           //GPS ALREDY TURNED ON
                    if (locationServiceConnected) {
                        if (ActivityCompat.checkSelfPermission(enableGPSActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
                            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, enableGPSActivity.this);
                        }
                    }
                    else {
                        redirectStatus.setText("ERROR: Not able to connect to Location Service");
                        btnRetry.setVisibility(View.VISIBLE);
                    }
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:               //GPS NOT ALREDY TURNED ON, SHOW TURN ON DIALOG
                    try {
                        status.startResolutionForResult(enableGPSActivity.this, REQUEST_CHECK_SETTINGS); //CREATING DIALOG FOR GPS TURN ON
                    } catch (IntentSender.SendIntentException e) {
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:       //GPS CANNOT BE TURNED ON
                    redirectStatus.setText("Permission Available, not able to enable GPS");
                    btnRetry.setVisibility(View.VISIBLE);
                    break;
            }
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {             //GPS ENABLE RELATED METHOD, DIALOG FOR GPS TURN ON
    if (requestCode == REQUEST_CHECK_SETTINGS) {
        if (resultCode == Activity.RESULT_OK) {                 // GPS TURN ON ACCEPTED
            if (locationServiceConnected) {
                if (ActivityCompat.checkSelfPermission(enableGPSActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
                    redirectStatus.setText("Please Wait. Fetching Your Current Location");
                    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, enableGPSActivity.this);
                }
            }
        }
        if (resultCode == Activity.RESULT_CANCELED) {               //GPS TURN ON REJECTED
            redirectStatus.setText("Permission Available, GPS Off");
            btnRetry.setVisibility(View.VISIBLE);
        }
    }
}

}

2 个答案:

答案 0 :(得分:1)

  //  declares variables name 

        private AlertDialog gpsAlertDialog;
        private boolean  isGpsDialogShowing = false;
        private LocationManager manager;


      // check gps enable or not if enable than remove dialog else show gps enable dialog in onResume method


protected void onResume() 
        {          
    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)&&!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                    ShowGpsDialog();
                } else {
                    removeGpsDialog();
                }


            }


 //   remove dialog on destroy activity 

    @Override
        protected void onDestroy() {
            super.onDestroy();
            removeGpsDialog();
        }


 //   method for show dialog for gps enable    


          private void ShowGpsDialog() {

                isGpsDialogShowing = true;
                AlertDialog.Builder gpsBuilder = new AlertDialog.Builder(
                        MainMenuDrawerActivity.this);
                gpsBuilder.setCancelable(false);
                gpsBuilder
                        .setTitle(getString(R.string.dialog_no_gps))
                        .setMessage(getString(R.string.dialog_no_gps_messgae))
                        .setPositiveButton(getString(R.string.dialog_enable_gps),
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                                        int which) {
                                        // continue with delete
                                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                        startActivity(intent);
                                        removeGpsDialog();
                                    }
                                })

                        .setNegativeButton(getString(R.string.dialog_exit),
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                                        int which) {
                                        // do nothing
                                        removeGpsDialog();
                                        finish();
                                    }
                                });
                gpsAlertDialog = gpsBuilder.create();
                gpsAlertDialog.show();
            }



      //  method for remove gps dialog 


            private void removeGpsDialog() {
                if (gpsAlertDialog != null && gpsAlertDialog.isShowing()) {
                    gpsAlertDialog.dismiss();
                    isGpsDialogShowing = false;
                    gpsAlertDialog = null;

                }
            }

答案 1 :(得分:1)

enableGPS();调用onCreate()而不是onStart()
因为,

根据Activity - 当用户按下主页按钮的生命周期,当前活动将通过调用 - onPause()和{{}进入后台 {1}}方法当您从Stack 恢复活动时 - onStop()将通过调用ActivityonRestart()onStart() 恢复