1

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

}

}

user2319636
  • 61
  • 1
  • 1
  • 4
  • post your code and trace. – NikolaB Apr 25 '13 at 12:14
  • Can you upload your code with error log ? – Lucifer Apr 25 '13 at 12:14
  • Please try to post some code with error log. – kamil Apr 25 '13 at 12:18
  • Use Alarm Manager to monitor the current location using service. You can refer here.http://stackoverflow.com/questions/2775628/android-how-to-periodically-send-location-to-a-server http://stackoverflow.com/questions/9407309/send-current-location-to-server-periodically-in-android – Shadow Apr 25 '13 at 12:20

0 Answers0