2016-02-28 87 views
0

編輯Margaret Bloom's link正是我所期待的。如何在用戶選擇「永不」允許之後提示用戶授予位置權限?

我正在寫一個應用程序,根據你當時的位置推薦食物,所以沒有位置信息,我的應用程序基本上是無用的。以下是我以編程方式訪問位置數據的方式。

public void onConnected(Bundle bundle) { 
    //adds the location request 
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocReq); 

    //make sure the phone is giving us what we asked for 
    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: //we have what we want 
        Log.v("Location settings", "Location settings are satisfied."); 
        startLocationUpdates(); 
        break; 
       case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
        Log.v("Location Settings", "Location settings not satisfied. Starting REQUEST_CHECK_SETTINGS"); 
        //we need to ask user before phone will give location information 
        try { 
         status.startResolutionForResult(LaunchActivity.this, REQUEST_CHECK_SETTINGS); 
        } catch (IntentSender.SendIntentException e) { 
         // Ignore the error. 
        } 
        break; 
       case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
        // This phone can't give us what we need. 
        Log.e("Location Settings", "The user \"never\" wants to give us location info"); 
        try { 
         status.startResolutionForResult(LaunchActivity.this, REQUEST_CHECK_SETTINGS); 
        } catch (IntentSender.SendIntentException e) { 
        } 
        break; 
      } 
     } 
    }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
     case REQUEST_CHECK_SETTINGS: //if we got the request check settings callback 
      switch (resultCode) { 
       case Activity.RESULT_OK: 
        Log.i("Location Settings", "onActivityForResult returned RESUME_OK"); 
        startLocationUpdates(); 
        break; 
       case Activity.RESULT_CANCELED: 
        Log.i("Location Settings", "user did not want to give us location permissions"); 
        break; 
      } 
      break; 
    } 
} 

如果在對話框中的用戶挑選「從不」的LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:情況下被設置,手機不再允許我以顯示位置請求對話框。重新啓動應用程序並手動啓用位置數據也不起作用,並且我的應用程序無法發送位置更新。

因此,從編程方面和UX方面,我可以再次提示我的用戶給予位置權限的最佳方式是什麼?

+0

見[此](https://stackoverflow.com/questions/29861580/locationservices-settingsapi-reset-settings-change-不可用標誌) –

回答

0

如果他/她已選擇從不,您不能提示用戶再次詢問。你可以做的最好的是,你可以將用戶置於設置中的應用頁面。他/她必須手動授予權限。

Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, 
         Uri.fromParts("package", getPackageName(), null)); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent); 

實施例: Example of the answer

圖片來源:谷歌圖片