2016-03-21 5 views
0

ich den folgenden Code verwendet, die in Stapelüberlauf war es zu prüfen, ob GPS aktiviert oder deaktiviert ist auf einem GerätGPS Service Check, um zu überprüfen, ob GPS aktiviert oder deaktiviert ist auf einem Gerät

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 (Settings.SettingNotFoundException e) { 
      e.printStackTrace(); 
     } 

     return locationMode != Settings.Secure.LOCATION_MODE_OFF; 

    } else { 
     locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
     return !TextUtils.isEmpty(locationProviders); 
    } 


} 

Es funktioniert gut auf meinem Gerät (Huawei G610) und ich habe keine Probleme damit. Dann gab ich die Anwendung an einen Freund, der ein Samsung Galaxy Note verwendet, er sagt, dass es nicht benachrichtigt, ob der GPS-Dienst nicht aktiviert ist.

Diese Funktion wird zum Auslösen eines Alarmdialogs verwendet, wenn GPS nicht aktiviert ist, um den Benutzer zur Aktivierung aufzufordern.

Ich habe keine Ahnung, warum es nicht auf seinem Gerät funktioniert, Ihre Hilfe wird sehr geschätzt. Ich frage nach dem Grund dafür, dass ich nicht an einigen Geräten arbeite und an einigen arbeite.

Danke.

+0

Mögliche Duplikat [Wie finde ich heraus, ob das Set sollte GPS eines Android-Geräts ist aktiviert] (http://stackoverflow.com/questions/843675/how-do-i-find-out-if-the-gps-of-android-device-is-enabled) – Bharatesh

Antwort

0

Sie können überprüfen, ob der Standort aktiviert ist oder nicht, indem Sie den folgenden Code verwenden. Ich werde in allen Geräten arbeiten

LocationManager location_manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
     LocationListener listner = new MyLocationListener(); 


     boolean networkLocationEnabled = location_manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
     boolean gpsLocationEnabled = location_manager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

     if (networkLocationEnabled || gpsLocationEnabled) { 

      if (location_manager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER) && networkLocationEnabled) { 
       location_manager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 2000, 2000, listner); 
       location_manager 
         .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      } else if (location_manager.getAllProviders().contains(LocationManager.GPS_PROVIDER) && gpsLocationEnabled) { 
       location_manager.requestLocationUpdates(
         LocationManager.GPS_PROVIDER, 2000, 2000, listner); 
       location_manager 
         .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
2

Versuchen Sie es.
privater LocationManager lm;

lm = (LocationManager)   getActivity().getSystemService(getActivity().LOCATION_SERVICE); 
if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
    startActivity(intent); 
} 

Und Sie Manifest-Datei Erlaubnis Die wichtigsten Berechtigungen, die Sie brauchen, sind android.permission.ACCESS_COARSE_LOCATION oder android.permission.ACCESS_FINE_LOCATION