2009-06-26 7 views
27

Attualmente sto scrivendo un'app in 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 fare questo programmaticamente?Come abilitare a livello di programmazione GPS in Android Cupcake

+0

dipende dalla versione di Cupcake. 1.5 non lo consente apparentemente. – D3vtr0n

risposta

51

Non è possibile, a partire da Android 1.5. Il massimo che puoi fare è aprire l'attività per consentire all'utente di abilitarlo/disattivarlo. Utilizzare l'azione contenuta in android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS per creare un'intenzione per aprire questa attività.

+3

Perché è disabilitato? Perché non consentire agli sviluppatori di attivare questo? il widget Power Control può, quindi dovremmo essere in grado di farlo. Non pensi? –

+24

È disabilitato per motivi di privacy. Se l'utente vuole disattivare il GPS, l'utente dovrebbe avere il GPS spento, punto. – CommonsWare

+0

Questi ragazzi sembrano aver capito. Sfortunatamente l'apk è stato offuscato e non sono riuscito a capire come è stato realizzato. URL: https: //market.android.com/details? Id = at.abraxas.powerwidget.free & hl = it –

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

Questo codice non è stato compilato per me su Android 2.2, poiché .isProviderEnabled non è un metodo statico su LocationManager.Il codice di lavoro per me era il seguente (scuse per la formattazione) LocationManager locationManager = (LocationManager) getSystemService (LOCATION_SERVICE); if (! LocationManager.isProviderEnabled (LocationManager.GPS_PROVIDER)) { Intenzione myIntent = new Intent (Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity (myIntent); } –

-5

se la tua domanda è a livello di utente di Android queste proprietà sono situate in: "Settings -> Location -> Use wireless networks" -> "Settings -> Location -> Use GPS satellites".

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

+0

Questa non è la risposta, la persona che pone la domanda desiderata –

3

si potrebbe utilizzare il seguente:

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 a livello di sistema di protezione firma. Quindi è necessario per cucinare la propria immagine ad usarla:/

6

Questo codice metodo può essere di aiuto per voi

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(); 
    } 
} 
+2

sì ** questo può essere fatto fino a 2,2 (sdk 8) **. Per maggiori informazioni, vedi [Abilita GPS come Programatically Tasker] (http://stackoverflow.com/a/5305835/383414) –

0

Si dovrebbe usare il Location Settings Dialog in Play Services, che richiede all'utente di abilitare servizi di localizzazione (se necessario) con un solo clic.

1

Innanzitutto verificare se il servizio di localizzazione è già attivo o no ??

Controllo servizio di localizzazione è attivata o non

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; 

} 

Poi finalmente aprire se il servizio posizione spenta precedentemente

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(); 
       }