Kontinuierliche GPS positionen

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Hm, ich habe den implementierten Listener im Konstuktor übergeben, hier mein Code:

    Java-Quellcode: GPSTracker.java

    1. package com.testgps.service;
    2. import android.app.Service;
    3. import android.content.Context;
    4. import android.content.Intent;
    5. import android.content.pm.PackageManager;
    6. import android.location.Location;
    7. import android.location.LocationListener;
    8. import android.location.LocationManager;
    9. import android.os.Build;
    10. import android.os.Bundle;
    11. import android.os.IBinder;
    12. import android.support.v4.content.ContextCompat;
    13. import android.util.Log;
    14. import com.testgps.util.Logger;
    15. /**
    16. * GPSTracker
    17. */
    18. public class GPSTracker extends Service implements LocationListener {
    19. private static final String LOG_TAG = GPSTracker.class.getSimpleName();
    20. private final Context mContext;
    21. // flag for GPS status
    22. boolean isGPSEnabled = false;
    23. // flag for network status
    24. boolean isNetworkEnabled = false;
    25. // flag for GPS status
    26. boolean canGetLocation = false;
    27. Location location; // location
    28. double latitude; // latitude
    29. double longitude; // longitude
    30. // The minimum distance to change Updates in meters
    31. private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 1 meter
    32. // The minimum time between updates in milliseconds
    33. private static final long MIN_TIME_BW_UPDATES = 1000; // 1 sec //minute
    34. // Declaring a Location Manager
    35. protected LocationManager locationManager;
    36. public GPSTracker(Context context) {
    37. this.mContext = context;
    38. getLocation();
    39. }
    40. public Location getLocation() {
    41. try {
    42. locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    43. // Register the listener with the Location Manager to receive location updates
    44. locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    45. // getting GPS status
    46. if (Build.VERSION.SDK_INT < 23 || (Build.VERSION.SDK_INT >= 23 &&
    47. ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
    48. ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
    49. isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    50. }
    51. // getting network status
    52. isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    53. if (!isGPSEnabled && !isNetworkEnabled) {
    54. // no network provider is enabled
    55. } else {
    56. this.canGetLocation = true;
    57. // First get location from Network Provider
    58. if (isNetworkEnabled) {
    59. locationManager.requestLocationUpdates(
    60. LocationManager.NETWORK_PROVIDER,
    61. MIN_TIME_BW_UPDATES,
    62. MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    63. Log.d("Network", "Network");
    64. if (locationManager != null) {
    65. location = locationManager
    66. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    67. if (location != null) {
    68. latitude = location.getLatitude();
    69. longitude = location.getLongitude();
    70. }
    71. }
    72. }
    73. // if GPS Enabled get lat/long using GPS Services
    74. if (isGPSEnabled) {
    75. if (location == null) {
    76. locationManager.requestLocationUpdates(
    77. LocationManager.GPS_PROVIDER,
    78. MIN_TIME_BW_UPDATES,
    79. MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    80. Log.d("GPS Enabled", "GPS Enabled");
    81. if (locationManager != null) {
    82. location = locationManager
    83. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    84. if (location != null) {
    85. latitude = location.getLatitude();
    86. longitude = location.getLongitude();
    87. }
    88. }
    89. }
    90. }
    91. }
    92. } catch (Exception e) {
    93. e.printStackTrace();
    94. }
    95. Logger.log(LOG_TAG, "************* getLocation " + location.getLatitude() + " " + location.getLongitude(), Logger.LogType.DEBUG);
    96. return location;
    97. }
    98. /**
    99. * Stop using GPS listener
    100. * Calling this function will stop using GPS in your app
    101. * */
    102. public void stopUsingGPS(){
    103. if(locationManager != null){
    104. if (Build.VERSION.SDK_INT < 23 || (Build.VERSION.SDK_INT >= 23 &&
    105. ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
    106. ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
    107. locationManager.removeUpdates(GPSTracker.this);
    108. }
    109. }
    110. }
    111. /**
    112. * Function to get latitude
    113. * */
    114. public double getLatitude(){
    115. if(location != null){
    116. latitude = location.getLatitude();
    117. }
    118. // return latitude
    119. return latitude;
    120. }
    121. /**
    122. * Function to get longitude
    123. * */
    124. public double getLongitude(){
    125. if(location != null){
    126. longitude = location.getLongitude();
    127. }
    128. // return longitude
    129. return longitude;
    130. }
    131. /**
    132. * Function to check GPS/wifi enabled
    133. * @return boolean
    134. * */
    135. public boolean canGetLocation() {
    136. return canGetLocation;
    137. }
    138. /**
    139. * Function to show settings alert dialog
    140. * On pressing Settings button will lauch Settings Options
    141. * */
    142. public void showSettingsAlert(){
    143. Logger.log(LOG_TAG, "~~~~~~~~ showSettingsAlert ~~~~~~~", Logger.LogType.DEBUG);
    144. // GPSLocationAccessDialog
    145. }
    146. @Override
    147. public void onLocationChanged(Location location) {
    148. Logger.log(LOG_TAG, "************* onLocationChanged " + location.getLatitude() + " " + location.getLongitude(), Logger.LogType.DEBUG);
    149. }
    150. @Override
    151. public void onProviderDisabled(String provider) {
    152. }
    153. @Override
    154. public void onProviderEnabled(String provider) {
    155. }
    156. @Override
    157. public void onStatusChanged(String provider, int status, Bundle extras) {
    158. }
    159. @Override
    160. public IBinder onBind(Intent arg0) {
    161. return null;
    162. }
    163. }
    Alles anzeigen
    Aber meine Debugausgabe meldet alle 4 minuten eine neue Location?
    Ich registriere beim Callback in den Zeilen 56 und 79

    Was 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.
    MfG,
    Christopher

    Eine gewisses Maß an Freundlichkeit kann man auch von Menschen im Internet erwarten.
    Das Forum basiert komplett auf der Freiwilligkeit ihrer Nutzer und diese sollen sich wohlfühlen! Daher seid bitte freundlich. Danke