2017-01-27 7 views
0

Итак, я пытаюсь создать приложение в Android Studio, для которого необходимо установить параметры местоположения, и поэтому я хотел, чтобы это окно появилось в onCreate, которое спрашивает, хотите ли вы включить настройки местоположения. Но после нажатия Abort я не знаю, как вернуть это окно обратно ... Есть ли способ сделать это? Потому что я видел в некоторых приложениях, таких как Tinder, что они не начнутся, пока вы не нажмете несколько раз на кнопку Abort.Постоянный запрос местоположения при запуске

LocationRequest mLocationRequest; 
    GoogleApiClient mGoogleApiClient; 
    PendingResult<LocationSettingsResult> result; 
    final static int REQUEST_LOCATION = 199; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(LocationServices.API) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this).build(); 
     mGoogleApiClient.connect(); 

    } 

    @Override 
    public void onConnected(Bundle bundle) { 

     mLocationRequest = LocationRequest.create(); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     mLocationRequest.setInterval(30 * 1000); 
     mLocationRequest.setFastestInterval(5 * 1000); 

     LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
       .addLocationRequest(mLocationRequest); 
     builder.setAlwaysShow(true); 

     result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); 

     result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
      @Override 
      public void onResult(LocationSettingsResult result) { 
       final Status status = result.getStatus(); 
       Log.d("Status: ", result.toString()); 
       //final LocationSettingsStates state = result.getLocationSettingsStates(); 
       switch (status.getStatusCode()) { 
        case LocationSettingsStatusCodes.SUCCESS: 
         // All location settings are satisfied. The client can initialize location 
         // requests here. 
         //... 
         break; 
        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
         // Location settings are not satisfied. But could be fixed by showing the user 
         // a dialog. 
         try { 
          // Show the dialog by calling startResolutionForResult(), 
          // and check the result in onActivityResult(). 
          status.startResolutionForResult(
            MainActivity.this, 
            REQUEST_LOCATION); 
         } catch (IntentSender.SendIntentException e) { 
          // Ignore the error. 
         } 
         break; 
        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
         // Location settings are not satisfied. However, we have no way to fix the 
         // settings so we won't show the dialog. 
         //... 
         break; 
       } 
      } 
     }); 

    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     Log.d("onActivityResult()", Integer.toString(resultCode) + "," + Integer.toBinaryString(requestCode)); 

     switch (requestCode) 
     { 
      case REQUEST_LOCATION: 
       switch (resultCode) 
       { 
        case Activity.RESULT_OK: 
        { 
         // All required changes were successfully made 
         Toast.makeText(MainActivity.this, "Location enabled by user!", Toast.LENGTH_LONG).show(); 
         break; 
        } 
        case Activity.RESULT_CANCELED: 
        { 
         // The user was asked to change settings, but chose not to 
         Toast.makeText(MainActivity.this, "Location not enabled, user cancelled.", Toast.LENGTH_LONG).show(); 
         break; 
        } 
        default: 
        { 
         break; 
        } 
       } 
       break; 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 
+0

Post соответствующие фрагменты кода. –

+0

Я отправил код сейчас – HeatTheIce

ответ

0

Создать METHODE

private void mGoogleApiClientConnect(){ 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this).build(); 
    mGoogleApiClient.connect(); 
} 

и назвать его здесь:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mGoogleApiClientConnect(); 
} 

и здесь:

case Activity.RESULT_CANCELED: 
    { 
      Toast.makeText(MainActivity.this, "Location not enabled, user cancelled.", Toast.LENGTH_LONG).show(); 
      mGoogleApiClient.connect(); 
      break; 
    }