2017-03-02 21 views
0

我正在開發Android KitKat版本(最低API級別19)的Android應用程序,在該應用程序中我需要自動打開用戶設備的位置訪問選項而不詢問用戶。我試過這段代碼(下面給出),但它彈出一個對話框並詢問用戶。所以請告訴我如何在不詢問用戶的情況下打開設備的位置訪問權限。在不詢問用戶的情況下打開設備的位置訪問選項

代碼:

public static void enableDeviceLocation(final Activity activity) { 

    final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(activity.getApplicationContext()).addApi(LocationServices.API).build(); 
    googleApiClient.connect(); 
    LocationRequest locationRequest = LocationRequest.create(); 
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest); 
    builder.setAlwaysShow(true); 
    PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build()); 

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
     @Override 
     public void onResult(LocationSettingsResult result) { 
      Status status = result.getStatus(); 
      switch (status.getStatusCode()) { 
       case LocationSettingsStatusCodes.SUCCESS: 
        //All location settings are satisfied. 
        break; 
       case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
        //Location settings are not satisfied. Show the user a dialog to upgrade location settings 
        try { 
         // Show the dialog by calling startResolutionForResult(), and check the result in onActivityResult(). 
         status.startResolutionForResult(activity, LocationRequest.PRIORITY_HIGH_ACCURACY); 
        } 
        catch (IntentSender.SendIntentException e) { 
         //PendingIntent unable to execute request. 
        } 
        break; 
       case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
        //Location settings are inadequate, and cannot be fixed here. Dialog not created. 
        break; 
      } 
      googleApiClient.disconnect(); 
     } 
    }); 
} 
+4

簡單地說,你不能 – tyczj

+2

你不能,爲了安全。否則,您可以跟蹤未被注意的用戶。這就是爲什麼在更新的版本中,你甚至不得不在運行時問。 –

回答

3

在一個標準的無根的Android設備,您不能自動無需用戶的參與使任何位置提供,顯而易見的隱私和安全方面的原因。

相關問題