NullpointerException when calling getSystemService (Context.LOCATION_SERVICE) and onLocationChanged not called

I am trying to create a background service that updates the current gps position. I get NullPointerExceptionin lineLocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

HomeActivity Launches Service

startService(new Intent(getApplicationContext(), ForHire.class));

Service (ForHire) creates TimerTask updates

public class ForHire extends Service {

...
private Timer getUpdatesNow = new Timer();
private Updates updates = new Updates(getUpdatesNow);

@Override
public void onCreate() {
    ...
    getUpdatesNow.schedule(updates, God.KM20TIME);
    Log.v("Taxeeta", "Notification created");
}

    private class Updates extends TimerTask implements LocationListener {
    private Timer getUpdatesNow;

    public Updates(Timer newGetUpdatesNow) {
        super();
        getUpdatesNow = newGetUpdatesNow;
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                God.KM20TIME, God.KM20DISTANCE, (LocationListener) this);
    }
    public void run() {
        ...
        //do some cleanup
    }
            @Override
    public void onLocationChanged(Location location) {
        Log.v("Taxeeta", "Location changed");
        // do a update of the current location.
    }

The first problem is that I get this NullPointerException. The second problem is that onLocationChanged is never called if I comment on LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, God.KM20TIME, God.KM20DISTANCE, (LocationListener) this);these two lines.

My manifest

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application ...>
   ...
   <uses-library android:name="com.google.android.maps" />
</application>

What am I missing here?

: Values of KM20TIME = 5000 (5 ) KM20DISTANCE = 1 (1 ). , , GPS . , gps (LSB) , 5 .

+5
3

, new Updates(getUpdatesNow);, onCreate(). , getSystemService() , onCreate().

Try:

private Timer getUpdatesNow;
private Updates updates;

@Override
public void onCreate() {
    ...
    getUpdatesNow = new Timer();
    updates = new Updates(getUpdatesNow);
+11
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

// Define a listener that responds to location updates
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (location != null) {
    Toast.makeText(getBaseContext(),
                            "Last Location is found..",
                            Toast.LENGTH_LONG).show();  
} else {

    Toast.makeText(getBaseContext(),
                            "Last Location is not found..",
                            Toast.LENGTH_LONG).show();
}
LocationListener locationListener = new LocationListener() {
public void onStatusChanged(String provider,
    int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
    Toast.makeText(getApplicationContext(),
        "Gps Enabled", Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider) {
    Toast.makeText(getApplicationContext(),
        "Gps Disabled", Toast.LENGTH_SHORT).show();
}

@Override
public void onLocationChanged(Location location) {
    Log.i("geolocation",
        "lat is : " + location.getLatitude()
        + " and " + location.getLongitude()
        + "");
    Toast.makeText(getBaseContext(),
    "Location is Changed..", Toast.LENGTH_LONG)
        .show();
}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0,
locationListener);
+3
@Override
public void onCreate() {
     new Updates(getUpdatesNow);
    ...
    getUpdatesNow.schedule(updates, God.KM20TIME);
    Log.v("Taxeeta", "Notification created");
}
+2

All Articles