2016-05-26 17 views
0

Ich verwende diesen Code, damit der Benutzer die Standortdienste aktiviert. Ich möchte den Benutzerdialog anzeigen, der akzeptiert wird, um die Standortdienste zu aktivieren, und sie dann auf die Seite LOCATION_SETTINGS umleiten. Aber ohne dass der Benutzer es vom Benutzer akzeptiert, leitet es die Standorteinstellungen um.Anfordern von Standortdiensten Android

if(lm==null) 
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
try{ 
    gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
}catch(Exception ex){} 
try{ 
    network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
}catch(Exception ex){} 

if(!gps_enabled && !network_enabled){ 
    dialog = new AlertDialog.Builder(this); 
    dialog.setMessage(this.getResources().getString(R.string.gps_network_not_enabled)); 
    dialog.setPositiveButton(this.getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
      // TODO Auto-generated method stub 
      Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      startActivity(myIntent); 
      //get gps 
     } 
    }); 
    dialog.setNegativeButton(this.getString(R.string.Cancel), new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
      // TODO Auto-generated method stub 
      finish(); 
     } 
    }); 
    dialog.show(); 

} 

Antwort

0

Soweit ich verstehe, versuchen Sie, den Standort zu aktivieren.

Sie können einen Dialog anzeigen, um den Standort zu aktivieren, anstatt ihn zur LOCATION_SETTING-Seite umzuleiten.

Schritt für Schritt Prozess.

Zunächst müssen Sie einen GoogleApiClient einrichten und GoogleApiClient CallBackMethods implementieren.

protected synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new Builder(this) 
       .addApi(LocationServices.API) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API).build(); 
     mGoogleApiClient.connect(); 
} 

Callback-Methoden

@Override 
public void onConnected(Bundle bundle) { 
    Log.d("OnConnected", "Connection successful"); 
    settingsrequest(); 
} 

@Override 
public void onConnectionSuspended(int i) { 
    mGoogleApiClient.connect(); 
} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + connectionResult.getErrorCode()); 
} 

Sobald die googleapiclient verbindet, wird onConnected Methode aufrufen und settingsRequest Methode aufrufen wird.

protected static final int REQUEST_CHECK_SETTINGS = 0x1; 
LocationRequest locationRequest; 


public void settingsrequest() { 
    locationRequest = LocationRequest.create(); 
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
    locationRequest.setInterval(30 * 1000); 
    locationRequest.setNumUpdates(1); 
    locationRequest.setExpirationDuration(20000); 
    locationRequest.setFastestInterval(500); 
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest); 
    builder.setAlwaysShow(true); //this is the key ingredient 
    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: { 
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this); 
       } 
       // 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(MainTab.this, REQUEST_CHECK_SETTINGS); 
        } 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; 
      } 
     } 
    }); 
} 

status.startResolutionForResult (MainTab.this, REQUEST_CHECK_SETTINGS) wird ein Dialog anfordernden Stelle zeigen.

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
    // Check for the integer request code originally supplied to startResolutionForResult(). 
     case REQUEST_CHECK_SETTINGS: 
      switch (resultCode) { 
       case Activity.RESULT_OK: 
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this); 
        break; 
       case Activity.RESULT_CANCELED: 
        break; 
      } 
      break; 
    } 
} 

LocationServices.FusedLocationApi.requestLocationUpdates (mGoogleApiClient, Location, this) wird ein Listener für den Standort gesetzt cahnged.

Wenn der Speicherort onLocationChanged-Methode geändert wird aufgerufen.

@Override 
public void onLocationChanged(Location location) { 
     prevLocation = location; 
     //Do you work 
} 

hoffen, dass dies Ihnen hilft.

Verwandte Themen