3

I am a beginner in Android development. I'm creating a small app that takes a picture after a device reaches a certain speed. However, one of the parameters of the requestLocationUpdates method (which of course is used to track the user’s location), requires a Looper in its third prameter. How would I create a looper? And where should it go? My requestLocationUpdates method currently looks like this: (I have null in place of a Looper)

 protected void requestUpdates() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            MC.requestLocationUpdates(locRequest, mLocationCallback, null);
        }
    }

The Android dev site has advised to do something like this:

 class LooperThread extends Thread {
      public Handler mHandler;

      public void run() {
      Looper.prepare();

      mHandler = new Handler() {
          public void handleMessage(Message msg) {
              // process incoming messages here
          }
      };

      Looper.loop();
      }
  }

However, I still have no clue where to put this code, what to do with it once I put it in, or how I can use it within my MainActivity. Any suggestions will be greatly appreciated.

Daniel
  • 137
  • 2
  • 11
  • "_However, one of the parameters it requires is a Looper_" Getting a location does not require a Looper. Can you clarify what requires a Looper ? – Sharp Edge Feb 03 '18 at 18:13
  • The third parameter of the requestLocationUpdates method requires a Looper. I clarified it in the question as well. Thanks for the comment. – Daniel Feb 03 '18 at 18:56
  • 1
    There are many versions of `requestLocationUpdates()`. You probably want to use the one that takes a `LocationListener` as a parameter. No `Looper` is needed. – Markus Kauppinen Feb 03 '18 at 20:09

3 Answers3

2

Create a looper by following code.

    HandlerThread handlerThread = new HandlerThread("MyHandlerThread");
    handlerThread.start();
    // Now get the Looper from the HandlerThread
    // NOTE: This call will block until the HandlerThread gets control and initializes its Looper
    Looper looper = handlerThread.getLooper();

and pass object of looper in requestLocationUpdates() as peramiter as following.

MC.requestLocationUpdates(locRequest, mLocationCallback, looper); 
1

If your callback does not access disk and is quick you can pass null.

Quoting documentation, you should pass

a Looper object whose message queue will be used to implement the callback mechanism, or null to make callbacks on the calling thread

If you call requestLocationUpdates on the main thread and pass null as Looper, you'll receive callbacks on the main thread.

You can't access network from the main thread and you shouldn't perform any I/O operations on the main thread. Your UI would start lagging.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
0

As @Markus said in his comments, you will probably be required to pass this if your Activity has implemented LocationListener interface. That is the 3rd parameter you will need instead of a Looper.

Sharp Edge
  • 4,144
  • 2
  • 27
  • 41