I have a service within an android app which implements locationListener to keep track of users changing location.
Every time the location is changed, I want to send it to the server.
So, in the onLocationChanged method I am fetching the latitude and longitude. I want to send them to the server over network. Hence, I created a new thread (as network operations are not allowed on main thread). However, I get a java.lang.NullPointerException for this thread.
What should I do?
Here's my code .....
public class SService extends Service implements LocationListener{
public class LocalBinder extends Binder {
SplashService getService() {
return SplashService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//Shows notification
showNotification();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("SService", "Received start id " + startId + ": " + intent);
// Sets parameters and location providers
setLocationParam();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
// Cancel the persistent notification.
mNM.cancel(NOTIFICATION);
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();
@Override
public void onLocationChanged(Location location) {
// Getting latitude of the current location
latitude = location.getLatitude();
// Getting longitude of the current location
longitude = location.getLongitude();
Log.v("SSERVICE",latitude+" "+longitude);
final SMessage sm=new SMessage("0", latitude, longitude,"");
// Creates new thread
new SThread(sm).start();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}