简体   繁体   中英

Is there something wrong with my OnLocationChanged?

My location Logger Service class does not provide me with updated informations about my location. Is there something wrong with me OnLocationChanged?

public class LocationLoggerService extends Service implements LocationListener {

    Location location; // location
    String towers;

    // Declaring a Location Manager
    public LocationManager locationManager;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        ourLocation();
        Criteria crit = new Criteria();
        towers = locationManager.getBestProvider(crit, false);
        location = locationManager.getLastKnownLocation(towers);

        // Just af test here:
        String hej1 = Double.toString(location.getLatitude());
        String hej2 = Double.toString(location.getLongitude());
        Toast.makeText(this, "Fra Oncreate: /nLat: " + hej1 + "Long: " + hej2,
                Toast.LENGTH_LONG).show();

    }

    public void ourLocation()

    {
        // Find my current location
        locationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                2000, 0, this);
    }

onLocationChanged method does not provide me with new informations. I tried out with some test(toast), but its like it does not read this method.

@Override
public void onLocationChanged(Location loc) {
    if (loc != null) {
        // Testing the position
        String hej3 = Double.toString(loc.getLatitude());
        String hej4 = Double.toString(loc.getLongitude());
        Toast.makeText(this, "loc: " + "Lat: " + hej3 + "Long: " + hej4,
                Toast.LENGTH_LONG).show();
    }
}

    @Override
    public void onProviderEnabled(String s) {
    }

    @Override
    public void onProviderDisabled(String s) {
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle b) {
    }

    @Override
    public void onDestroy() {

        // TODO Auto-generated method stub

        super.onDestroy();

        Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG)
                .show();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // this service will run until we stop it

        return START_STICKY;
    }

}

My manifest

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />


<service
        android:name="LocationLoggerService"
        android:enabled="true"
        android:exported="false"
        android:label="LocationLoggerService" />
</application>

Try to change the code as below and try again, get the location object directly from the onLocationChanged callback method... I am hoping you are moving to test this functionality. location ojbect will contains the latest latitude and longitude.

@Override
public void onLocationChanged(Location location) {


    // Testing onLocationChanged:
    String hej1 = Double.toString(location.getLatitude());
    String hej2 = Double.toString(location.getLongitude());
    Toast.makeText(this, "Location: "+"Lat: " + hej1 + "Long: " + hej2,
            Toast.LENGTH_LONG).show();

    String hej3 = Double.toString(loc.getLatitude());
    String hej4 = Double.toString(loc.getLongitude());
    Toast.makeText(this, "loc: " +"Lat: " + hej3 + "Long: " + hej4,
            Toast.LENGTH_LONG).show();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM