Melih Mucuk
Melih Mucuk

Reputation: 7066

Get Current Location From Android Background Service Issue

I want to develop location based reminder app. Therefore I want to use android service for get current location even app is not running. But I didn't do. I set timer in android service class but I don't know how to get current location in service class. What is the problem ? I got some error like this:

can't create handler inside thread that has not called looper.prepare()

public class TrackerService extends Service {

    double latShared;
    double lngShared;
    double latService;
    double lngService;
    private LocationManager lm;
    Timer timer;
    Handler handler;
    final static long TIME = 15000;
    SharedPreferences mSharedPrefs;
    SharedPreferences.Editor mPrefsEditor;



    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        mSharedPrefs = getSharedPreferences("locationXML", MODE_PRIVATE);
        latShared = (double)mSharedPrefs.getFloat("lat", 0);
        lngShared = (double)mSharedPrefs.getFloat("lng", 0);
        timer = new Timer();
        timer.schedule(new TimerTask(){
            @Override
            public void run(){
                LocationUpdates();
            }
        },0,TIME);

    }    

    public void LocationUpdates(){
        locListener locList = new locListener();
        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);   
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locList);
    }

    @Override
    public void onDestroy() {
        //lm.removeUpdates(this);
        timer.cancel();
    }

    public class locListener implements LocationListener{

        @Override
        public void onLocationChanged(Location location) {
            latService = location.getLatitude();
            lngService = location.getLongitude();
        }

        @Override
        public void onProviderDisabled(String provider) {}

        @Override
        public void onProviderEnabled(String provider) {}

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}


    }

}

Upvotes: 1

Views: 4273

Answers (1)

waqaslam
waqaslam

Reputation: 68167

LocationManager.requestLocationUpdates is supposed to run on UI thread as its sibling method defines. On the other hand TimerTask introduces a new thread to offload its execution.

However, if you are not using a new process for your service then simply call LocationUpdates() as below:

@Override
protected void onCreate() {
    mSharedPrefs = getSharedPreferences("locationXML", MODE_PRIVATE);
    latShared = (double)mSharedPrefs.getFloat("lat", 0);
    lngShared = (double)mSharedPrefs.getFloat("lng", 0);

    final Handler h = new Handler();
    h.post(new Runnable() {
        @Override
        public void run() {
            LocationUpdates();
            h.postDelayed(this, TIME);
        }
    });
}

OR, if you don't want to use handler then simply upgrade your requestLocationUpdates as below:

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locList, getMainLooper());

In above case, getMainLooper() will dispatch your location updates from UI thread.

Upvotes: 1

Related Questions