2017-11-17 2 views
0

私は、OKボタンとCANCELボタンでダイアログを表示することにより、onStart()でGPSを有効にするようにユーザーに要求するアクティビティを持っています。しかし、ユーザーがダイアログを操作する前にホームボタンを押して、アプリケーションを再起動すると、新しいダイアログボックスが古いダイアログボックスの上に表示され、ユーザーは2つのダイアログボックスを順番に表示します。アクティビティのonStopメソッドで古いダイアログボックスを閉じるにはどうすればよいですか?アクティビティが停止して再度開始された場合にGPSが2回発生するように要求します。

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); 
     } 
    } 
} 

}

答えて

1

コールenableGPS();onCreate()代わりのonStart()へ:

は、ここに私のコードの関連する部分です。 ユーザーがホームボタンを押すと、現在の活動の意志を呼び出すことにより、バックグラウンドに行く - - onPause()onStop()方法
Activityのライフサイクルによると

、理由は。 アクティビティをStackからActivityに再開すると、onRestart()onStart()およびonResume()と呼ぶことで、が再開されます。

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; 

       } 
      } 
関連する問題