Kontinuierliche GPS positionen
-
-
-
Hm, ich habe den implementierten Listener im Konstuktor übergeben, hier mein Code:
Java: GPSTracker.java
Alles anzeigenpackage com.testgps.service; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Build; import android.os.Bundle; import android.os.IBinder; import android.support.v4.content.ContextCompat; import android.util.Log; import com.testgps.util.Logger; /** * GPSTracker */ public class GPSTracker extends Service implements LocationListener { private static final String LOG_TAG = GPSTracker.class.getSimpleName(); private final Context mContext; // flag for GPS status boolean isGPSEnabled = false; // flag for network status boolean isNetworkEnabled = false; // flag for GPS status boolean canGetLocation = false; Location location; // location double latitude; // latitude double longitude; // longitude // The minimum distance to change Updates in meters private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 1 meter // The minimum time between updates in milliseconds private static final long MIN_TIME_BW_UPDATES = 1000; // 1 sec //minute // Declaring a Location Manager protected LocationManager locationManager; public GPSTracker(Context context) { this.mContext = context; getLocation(); } public Location getLocation() { try { locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); // Register the listener with the Location Manager to receive location updates locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); // getting GPS status if (Build.VERSION.SDK_INT < 23 || (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) { isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } // getting network status isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // no network provider is enabled } else { this.canGetLocation = true; // First get location from Network Provider if (isNetworkEnabled) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("Network", "Network"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } // if GPS Enabled get lat/long using GPS Services if (isGPSEnabled) { if (location == null) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("GPS Enabled", "GPS Enabled"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } } } catch (Exception e) { e.printStackTrace(); } Logger.log(LOG_TAG, "************* getLocation " + location.getLatitude() + " " + location.getLongitude(), Logger.LogType.DEBUG); return location; } /** * Stop using GPS listener * Calling this function will stop using GPS in your app * */ public void stopUsingGPS(){ if(locationManager != null){ if (Build.VERSION.SDK_INT < 23 || (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) { locationManager.removeUpdates(GPSTracker.this); } } } /** * Function to get latitude * */ public double getLatitude(){ if(location != null){ latitude = location.getLatitude(); } // return latitude return latitude; } /** * Function to get longitude * */ public double getLongitude(){ if(location != null){ longitude = location.getLongitude(); } // return longitude return longitude; } /** * Function to check GPS/wifi enabled * @return boolean * */ public boolean canGetLocation() { return canGetLocation; } /** * Function to show settings alert dialog * On pressing Settings button will lauch Settings Options * */ public void showSettingsAlert(){ Logger.log(LOG_TAG, "~~~~~~~~ showSettingsAlert ~~~~~~~", Logger.LogType.DEBUG); // GPSLocationAccessDialog } @Override public void onLocationChanged(Location location) { Logger.log(LOG_TAG, "************* onLocationChanged " + location.getLatitude() + " " + location.getLongitude(), Logger.LogType.DEBUG); } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public IBinder onBind(Intent arg0) { return null; } }
Aber meine Debugausgabe meldet alle 4 minuten eine neue Location?
Ich registriere beim Callback in den Zeilen 56 und 79Was mache ich falsch?
-
Also sehe jetzt nichts. Evtl. hat sich das GPS-Signal nicht bewegt in der Zeit oder aber du hast evtl. den Energiesparmodus an. Das wäre das was mir einfällt.
-
Hm ja thnx. Ich debugge am Rechner, dann lauf ich mal mit USB Kabel und Notebook rum
-
Es gibt eine app namens catlog, wenn du root hast, solltest du den Laptop nicht benötigen um die logcat einträge lesen zu können
Jetzt mitmachen!
Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!