
Meroelyth
Ich entwickle eine App auf Android OS. Ich weiß nicht, wie ich überprüfen kann, ob die Ortungsdienste aktiviert sind oder nicht.
Ich brauche eine Methode, die “true” zurückgibt, wenn sie aktiviert sind, und “false”, wenn nicht (also kann ich im letzten Fall einen Dialog anzeigen, um sie zu aktivieren).

Shankar Agarwal
Sie können den folgenden Code verwenden, um zu überprüfen, ob der GPS-Anbieter und die Netzwerkanbieter aktiviert sind oder nicht.
LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;
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) {
// notify user
new AlertDialog.Builder(context)
.setMessage(R.string.gps_network_not_enabled)
.setPositiveButton(R.string.open_location_settings, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton(R.string.Cancel,null)
.show();
}
Und in der Manifestdatei müssen Sie die folgenden Berechtigungen hinzufügen
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Slava-Tanne
Ich verwende diesen Code zur Überprüfung:
public static boolean isLocationEnabled(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (SettingNotFoundException e) {
e.printStackTrace();
return false;
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
}else{
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}

Mayank Sharma
Wie jetzt im Jahr 2020
Der neueste, beste und kürzeste Weg ist
@SuppressWarnings("deprecation")
public static Boolean isLocationEnabled(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// This is a new method provided in API 28
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return lm.isLocationEnabled();
} else {
// This was deprecated in API 28
int mode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,
Settings.Secure.LOCATION_MODE_OFF);
return (mode != Settings.Secure.LOCATION_MODE_OFF);
}
}

Sonnig
Auf AndroidX migrieren und verwenden
implementation 'androidx.appcompat:appcompat:1.3.0'
und verwenden Sie LocationManagerCompat
Auf Java
private boolean isLocationEnabled(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return LocationManagerCompat.isLocationEnabled(locationManager);
}
In Kotlin
private fun isLocationEnabled(context: Context): Boolean {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
return LocationManagerCompat.isLocationEnabled(locationManager)
}

Lenik
Sie können diesen Code verwenden, um Benutzer zu den Einstellungen zu leiten, wo sie GPS aktivieren können:
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ) {
new AlertDialog.Builder(context)
.setTitle(R.string.gps_not_found_title) // GPS not found
.setMessage(R.string.gps_not_found_message) // Want to enable?
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
owner.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton(R.string.no, null)
.show();
}

ZaBlanc
Ausgehend von der obigen Antwort müssen Sie in API 23 “gefährliche” Berechtigungsprüfungen hinzufügen und das System selbst überprüfen:
public static boolean isLocationServicesAvailable(Context context) {
int locationMode = 0;
String locationProviders;
boolean isAvailable = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
isAvailable = (locationMode != Settings.Secure.LOCATION_MODE_OFF);
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
isAvailable = !TextUtils.isEmpty(locationProviders);
}
boolean coarsePermissionCheck = (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED);
boolean finePermissionCheck = (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
return isAvailable && (coarsePermissionCheck || finePermissionCheck);
}

GoRoS
Ja, Sie können unten den Code überprüfen:
public boolean isGPSEnabled(Context mContext)
{
LocationManager lm = (LocationManager)
mContext.getSystemService(Context.LOCATION_SERVICE);
return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
mit der Erlaubnis in der Manifestdatei:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
9162600cookie-checkWie überprüfe ich, ob Ortungsdienste aktiviert sind?yes
Ich weiß, dass dies ein altes Thema ist, aber für diejenigen, die folgen mögen … Google hat dafür eine API veröffentlicht; sehen developer.google.com/android/reference/com/google/android/gms/…
– Peter McLennan
15. August 2015 um 3:02 Uhr
Ich habe eine ähnliche Frage hier mit Codes beantwortet. Hör zu. Sehr hilfreich.
– S. bruce
2. März 2016 um 19:16 Uhr
FYI: SettingsApi ist jetzt veraltet. Verwenden developer.google.com/android/reference/com/google/android/gms/… stattdessen.
– Rajiv
19. Mai 2020 um 6:49 Uhr