2

I am having an issue with my app. I need to get Location Updates at certain interval's and therefore basically need to be able to control the GPS module pretty well, this is not really easy with the Android OS. Basically i need to turn the GPS on for 2 min at 5 min interval's. I have the timing down, and i can get the location twice, but then the app crashes with the a RunTime Error - only one Looper may be created per thread. The timing is done in a service class and works well, it removes updates and everything its just this issue i am having.

I origninally had this error - "Can't create handler inside thread that has not called Looper.prepare()" which i fixed with the code below, but now i get the only one Looper error

My looper Thread looks like this (please don't be harsh, i am very new to Android lol)

            public void run() {
        Looper.prepare();
        setLooper(Looper.myLooper());
        LocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, locationListener);
        Timer t = new Timer();
        t.schedule(new TimerTask() {

            @Override
            public void run() {
                stopLooper();

            }

        }, TWO_MINUTES);
        Looper.loop();
        t.cancel();
        setLooper(null);
        vb.getLm().removeUpdates(ll);

    }

Like i said i am new, and i am not sure how to use a Handler. i did get some of this code from a post here at StackOverflow, just used it differently, but its no working.

Please i really need help. Thank you for any responses.

Ok i seem to have found the solution, just need to test it and then wait another 7 hours before i can post an answer lol. Thanks for any views and replies.

SeanSWatkins
  • 413
  • 4
  • 9

2 Answers2

1

Cant understand what your code doing :) Why not use something easier like this:

public void run( ) {
    while (true) {
        LocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, locationListener);
        sleep(2 * 60 * 1000);
        LocationManager.removeUpdates();
        sleep(5 * 60 * 1000);
    }
}

Ok, maybe something like that?

Runnable start = new Runnable( ) {
    public void run( ) {
        LocationManager.startLocationUpdates
        handler.postDelayed(stop, 2 * 60 * 1000L);
    }
}

Runnable stop = new Runnable( ) {
    public void run( ) {
        LocationManager.removeLocationUpdates
    }
}

Runnable onePeriod = new Runnable( ) {
    public void run( ) {
        handler.postDelayed(onePeriod, 5 * 60 * 1000);
        handler.post(start);
    }
}

public void startContiniuosListening( ) {
    handler.post(onePerion);
}

public void stopContiniousListening( ) {
    handler.removeCallback(stop);
    handler.removeCallback(onePeriod);
    LocationManager.removeLocationUpdates(...)
}

where handler is class field:

Handler handler = new Handler();
HighFlyer
  • 1,615
  • 2
  • 15
  • 22
  • Because although i need polling every 5 mun for 2 min i also need to be able to control when and where it must poll. Such as if a person is inside is must remain off till they are outside (i have that part working) but i need to get the Looper thing running – SeanSWatkins Dec 09 '11 at 08:23
  • I also don't know where or if i am supposed to call Thread.run() because i would presume it must be done once, but only once, otherwise is creates a new Looper. – SeanSWatkins Dec 09 '11 at 08:24
  • Lol, yeah. i figured it out very similar, but i controlled the timing in a separate class. Thanks so much for your help, i shall post my answer later too :-) – SeanSWatkins Dec 09 '11 at 10:20
0

I found this answer to a similar question to be very helpful: https://stackoverflow.com/a/6576972/588556

Basically, you just need to wrap the code in your question in its own thread...

new Thread(new Runnable() {
    // Your code here...
}).start();

Then this worker thread will create its own Looper.

Community
  • 1
  • 1
XtopherSD
  • 333
  • 2
  • 11
  • I managed to get it to work by just using a Handler, and posting a new handler at the time i needed another gps fix. It works well, don't know if it is very elegant, but it worked :-) – SeanSWatkins Dec 10 '12 at 20:18