5

Normally an error while doing something on the UI thread from another thread I thought, but I don't get what I'm doing wrong. The error seems only to appear when the phone is travelling, so with a changing GPS location.

I want the most recent location to be stored, so nothing on the UI. I have the following method called from the main activity:

    public void getFreshDeviceLocation(long interval, final long maxtime) {
            if (gps_recorder_running){return;}
              gpsTimer = new Timer();
            //starts location 
            startMillis=System.currentTimeMillis();

            // receive updates        
             for (String s : locationManager.getAllProviders()) {

                        LocationListener listener=new LocationListener() {


                            @Override
                            public void onProviderEnabled(String provider) {

                            }

                            @Override
                            public void onProviderDisabled(String provider) {

                            }

                            @Override
                            public void onLocationChanged(Location location) {
                                // if this is a gps location, we can use it
                                if (location.getProvider().equals(
                                        LocationManager.GPS_PROVIDER)) {
                                    doLocationUpdate(location, true); //stores the location in the prefs
                                    stopGPS();
                                }
                            }

                            @Override
                            public void onStatusChanged(String provider,
                                    int status, Bundle extras) {
                                // TODO Auto-generated method stub

                            }
                        };

                        locationManager.requestLocationUpdates(s, interval, //line 97, error!
                                minDistance, listener); 
                        myListeners.add(listener);

                    gps_recorder_running = true;
            }

            // start the gps receiver thread
            gpsTimer.scheduleAtFixedRate(new TimerTask() {

                @Override
                public void run() {
                    Location location = getBestLocation();
            doLocationUpdate(location, false);
            if ((System.currentTimeMillis()-startMillis)>maxtime){if (maxtime>0){stopGPS();}}
//Updates depend on speed of the device
            float speed=pref.DeviceLocation().getSpeed();
              if (speed<1){if (speedclass!=0){speedclass=0;stopGPS();getFreshDeviceLocation(300000,0);}}
              if ((speed>=1)&&(speed<3)){if (speedclass!=1){speedclass=1;stopGPS();getFreshDeviceLocation(90000,0);}}
              if ((speed>=3)&&(speed<17)){if (speedclass!=2){speedclass=2;stopGPS();getFreshDeviceLocation(15000,0);}}
              if (speed>=17){if (speedclass!=3){speedclass=3;stopGPS();getFreshDeviceLocation(10000,0);}}
                }
            }, 0, interval);

        }

The error I get is:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:139)
at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:137)
at android.location.LocationManager._requestLocationUpdates(LocationManager.java:708)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:630)
at com.appiclife.tmoflashlight.TMO_LocationManager.getFreshDeviceLocation(TMO_LocationManager.java:97)
at com.appiclife.tmoflashlight.TMO_LocationManager$2.run(TMO_LocationManager.java:116)
at java.util.Timer$TimerImpl.run(Timer.java:289)

Line 97=locationManager.requestLocationUpdates(s, interval,minDistance, listener); (LocationManager class)

Seems like I have to call Looper.prepare(); and Looper.loop(); somewhere, but I don't see where? It might have something to do with this question AsyncTask and Looper.prepare() error

Community
  • 1
  • 1
Diego
  • 4,011
  • 10
  • 50
  • 76

2 Answers2

8

In this case, I think what you need is a looper thread, this may help you.

Or you may create a handler inside your run() method make it a looper thread as the following example.

Handler mHandler;

public void run(){
  Looper.prepare();
  mHandler = new Handler(){
     public void handleMessage(Message msg){
        Looper.myLooper().quit();
     }       
  };
  //do your stuff
  //...
  mHandler.sendEmptyMessage(0); //send ourself a message so the looper can stop itself
  Looper.loop();
}//end of run

Please let me know if it helps :)

Community
  • 1
  • 1
Chen
  • 1,566
  • 1
  • 12
  • 16
  • Does not work. I got 2 compile errors. `Looper.quit()` and `mHandler.sendMessage(0);`. Instead the answer of jagdish works like a charm. – Niklas Nov 25 '13 at 19:13
  • @Niklas Sorry, whats the compiling error? (I did miss a before) – Chen Nov 26 '13 at 16:09
  • **Looper.quit();** `Cannot make a static reference to the non-static method quit() from the type Looper.` **mHandler.sendMessage(0);** `The method sendMessage(Message) in the type handler is not applicable for the arguments int` – Niklas Nov 26 '13 at 16:13
  • 1
    @Niklas I edited the code, thanks for pointing out the compiling error, somehow I forget to call myLooper first. Also you may want to refer to this [question](http://stackoverflow.com/questions/11407943/this-handler-class-should-be-static-or-leaks-might-occur-incominghandler) about the handler leak warning. – Chen Nov 26 '13 at 16:35
3
      runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            alert("Error: " + message);
        }
    });

You can use simply runOnUiThread to fix this issue. Thanks!!

Jagdish
  • 2,418
  • 4
  • 25
  • 51