2011-10-10 10 views

Odpowiedz

33

Możesz rozpocząć zamiar opcji dla ustawień lokalizacji.

Intent gpsOptionsIntent = new Intent( 
    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
startActivity(gpsOptionsIntent); 
+1

ale to spowoduje pętlę na ekranie ustawień, gdzie gdy idziesz wróć, nadal będzie wyświetlany ekran ustawień. –

+0

Dlaczego powoduje pętlę? –

+1

Myślę, że to zależy od tego, gdzie umieścisz ten kod. Jeśli zaczniesz działanie po każdym wznowieniu aplikacji, możesz skończyć w pętli. Ale nie jest to spowodowane samym kodem. –

5

można użyć

Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS); 
     startActivity(intent); 

po sprawdzeniu GPS.

kompletne źródło można znaleźć HERE lub można odwołać się do SO ANSWER

+0

zmiana ACTION_LOCATION_SOURCE_SETTINGS na ACTION_SECURITY_SETTINGS – Xenione

+0

Przydatne łącze do innej odpowiedzi głosowej! –

8

LOCATION_MODE będzie LOCATION_MODE_OFF jeśli GPS jest wyłączony, który zwróci wartość 0.

int off = Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE); 
    if(off==0){ 
     Intent onGPS = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     startActivity(onGPS); 
    } 
1

Wdrożenie LocationListner: tak

@Override 
public void onLocationChanged(Location location) { 

} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) 
{ 
if (provider.equals(LocationManager.GPS_PROVIDER)) 
{ 
    showGPSDiabledDialog(); 
} 
}  

Pokaż okno dialogowe, aby otworzyć Ustawienia:

public void showGPSDiabledDialog() { 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("GPS Disabled"); 
builder.setMessage("Gps is disabled, in order to use the application properly you need to enable GPS of your device"); 
builder.setPositiveButton("Enable GPS", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), GPS_ENABLE_REQUEST); 
    } 
}).setNegativeButton("No, Just Exit", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 

    } 
}); 
mGPSDialog = builder.create(); 
mGPSDialog.show(); 
} 

W swojej OnCreate wywołanie metody:

LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
{ 
    return; 
} 
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 

I ovveride onActivityResult sposób aby sprawdzić, czy włączona prawidłowo użytkownik gps

@Override 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
if (requestCode == GPS_ENABLE_REQUEST) 
{ 
    if (mLocationManager == null) 
    { 
     mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    } 

    if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
    { 
     showGPSDiabledDialog(); 
    } 
} 
else 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
} 
} 
Powiązane problemy