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() {
...
}
@Override
public void onLocationChanged(Location location) {
Log.v("Taxeeta", "Location changed");
}
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 .