使用locationlistener将当前位置发送到服务器

时间:2013-04-25 12:12:55

标签: android nullpointerexception locationlistener networkonmainthread

我在Android应用程序中有一个服务,它实现了locationListener来跟踪用户更改位置。

每次更改位置时,我都希望将其发送到服务器。

所以,在onLocationChanged方法中,我获取纬度和经度。我想通过网络将它们发送到服务器。因此,我创建了一个新线程(因为主线程上不允许网络操作)。但是,我得到了这个线程的java.lang.NullPointerException。

我该怎么办?

这是我的代码.....

    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

}

}

0 个答案:

没有答案