0

I am working on a location aware application and I have come to a standstill. What I want to do is be able to ask the Android device for a single GPS location and return it.

So far this is what I have. A service that is supposed to update the location periodically:

import android.app.Service;
import android.content.Context;
import android.content.Intent
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class KeepTime extends Service implements LocationListener {
float minAccuracyMeters = 20.0f;
LocationManager locationManager;
int index = 0;

int maxAmountOfTries = 5;
Timer timer;

boolean currentlyListening = false;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    System.out.println("Service started..");
    Toast.makeText(this, "Service started", Toast.LENGTH_SHORT).show();
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    if(!isLocationEnabled()){
        System.out.println("Location service is not active. Stopping self.");
        Toast.makeText(this, "Location service is not active. Stopping self.", Toast.LENGTH_SHORT).show();
        this.stopSelf();
        return 0;
    }

    timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {

            if (currentlyListening) {

            } else {
                setListener();
            }
        }
    }, 0, 1000 * 60 * 10);

    return Service.START_NOT_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();

    mll.removeListener();
    mll.cancelTimer();
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
    System.out.println("Service destroyed..");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

private boolean isLocationEnabled() {

    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
            locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}

private void setListener()
{
    System.out.println("Set listener called..");
    try {
        System.out.println("About to set listeners");
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        System.out.println("Location listeners set..");
        currentlyListening = true;
    }catch(SecurityException e){
        System.out.println("Security exception. "+e);
    }
}

public void removeListener() {
    try {
        locationManager.removeUpdates(this);
        System.out.println("Removed updates..");
        currentlyListening = false;
    } catch (SecurityException se) {
        System.out.println("Security exception: " + se);
    }
}

@Override
public void onLocationChanged(Location location) {
    System.out.println("Location changed..");
    double longitudeNetwork = location.getLongitude();
    double latitudeNetwork = location.getLatitude();
    double accuracy = location.getAccuracy();

    if(accuracy < minAccuracyMeters || index > maxAmountOfTries) {
        System.out.println("Accuracy is less than " + minAccuracyMeters + " Recording..");
System.out.println(longitudeNetwork+ " "+latitudeNetwork+ " "+accuracy);

        removeListener();
    }
    else{
        System.out.println("Accuracy was not good enough. Still recorded location.");

    }
    index++;
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    System.out.println("Status changed");
}

@Override
public void onProviderEnabled(String provider) {
    System.out.println("Provider enabled.");
}

@Override
public void onProviderDisabled(String provider) {
    System.out.println("Provider disabled.");
}
}

Every 10 minutes I want the location to be printed out when it reaches a certain accuracy or after a certain amount of time. But it throws and exception. Here is the exception:

FATAL EXCEPTION: Timer-0
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

I have tried calling the prepare but it just causes more havoc. Any ideas how to achieve what I am after?

Sigmundur
  • 725
  • 1
  • 8
  • 25
  • 2
    Possible duplicate of [Can't create handler inside thread that has not called Looper.prepare()](http://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare) – buczek Apr 08 '16 at 15:17
  • But I do not want to do anything on the UI thread for now. – Sigmundur Apr 08 '16 at 15:44

2 Answers2

1

TimerTask works on different thread, so maybe your problem refers to this

And check this project, seems like it's mentioning about your whole idea perfectly

Edit

Instead of wasting time, i show the key materials for him. For the first link i already told it's key.

TimerTask works on different thread

  • You can do same thing as accepted answer in the link. So you must run your code scope in main thread because service works on main thread too.
  • Or you can use handler.postDelayed instead of TimerTask.

And i did not directly forward you to wasteful links, because i analyze your whole code. And it's refers to perfect project of commonsguy on github. So you can eliminate your mistakes by looking commonsguy project.

Community
  • 1
  • 1
blackkara
  • 4,900
  • 4
  • 28
  • 58
  • Instead of adding direct links for an answer, please copy the important points from the links and include the links for further reference. This will ensure if a post or site is taken down that users are still able to find your answer helpful. – buczek Apr 08 '16 at 15:49
0

the locationmanager.requestlocationupdates has a parameter for interval on when to get location again.. try pressing ctrl and hover your mouse on requestlocationupdate syntax.

dione llorera
  • 357
  • 4
  • 14