Domanda

Attualmente sto scrivendo un'app su Android che funziona con il GPS. Al momento sono in grado di capire se il GPS è abilitato. Il mio problema è che voglio abilitare il GPS all'avvio dell'app se è disabilitato. Come posso farlo programmaticamente?

È stato utile?

Soluzione

Non puoi, a partire da Android 1.5. Il massimo che puoi fare è aprire l'attività per consentire all'utente di attivarla / disattivarla. Usa l'azione svolta in android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS per creare un Intento per aprire questa attività.

Altri suggerimenti

if(!LocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ))
{
    Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS );
    startActivity(myIntent);
}

Questo codice di metodo può esserti di aiuto

private void turnGPSOnOff(){
  String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
  if(!provider.contains("gps")){
    final Intent poke = new Intent();
    poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
    poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
    poke.setData(Uri.parse("3")); 
    sendBroadcast(poke);
    //Toast.makeText(this, "Your GPS is Enabled",Toast.LENGTH_SHORT).show();
  }
}

È possibile utilizzare quanto segue:

try {
  Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);
} catch (Exception e) {
  logger.log(Log.ERROR, e, e.getMessage());
}

ma funzionerà solo se si dispone del livello di protezione della firma del sistema. Quindi devi cucinare la tua immagine per usarla effettivamente: /

Prima controlla se il servizio di localizzazione è già attivo o meno ??

Verifica del servizio di localizzazione abilitato o meno

public boolean isLocationServiceEnabled(){
    LocationManager locationManager = null;
    boolean gps_enabled= false,network_enabled = false;

    if(locationManager ==null)
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    try{
        gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }catch(Exception ex){
        //do nothing...
    }

    try{
        network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }catch(Exception ex){
        //do nothing...
    }

    return gps_enabled || network_enabled;

}

Quindi finalmente aprire se il servizio di localizzazione è stato disattivato in precedenza

  if (isLocationServiceEnabled())) {
          //DO what you need...
     } else {
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setMessage("Seems Like location service is off,   Enable this to show map")
      .setPositiveButton("YES", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
                             Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                    startActivity(intent);
                                }
                            }).setNegativeButton("NO THANKS", null).create().show();
                }

Dovresti usare le Impostazioni posizione Finestra di dialogo in Play Services che richiede all'utente di abilitare i servizi di localizzazione (se necessario) con un solo clic.

se la tua domanda è a livello dell'utente di Android, queste proprietà si trovano in: "Settings -> Location -> Use wireless networks" -> "Settings -> Location -> Use GPS satellites".

Ma allo sviluppatore è possibile utilizzare la classe "android.provider.Settings.Secure" con le autorizzazioni appropriate.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top