Handler inside the thread

I am new to android, carry it with me.

I have a TimerTask for which I define run () inside the Service. Inside run () I call

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
    LOCATION_UPDATES_MIN_TIME_MS, LOCATION_UPDATES_MIN_DISTANCE_M, gpsListener);

and he complains that he cannot create a handler, since I consider it a background thread. How to solve it?

Edit: code snippet

locationTask = new TimerTask() {

        @Override
        public void run() {
            Log.d(Commands.TAG, "Running location Task");
            myLocationProvider = new MyLocationProvider(locationManager, handler, MyService.this);
            myLocationProvider.start();
            myLocationProvider.stop();
        }
    };

and then his schedule as follows:

locationTimer = new Timer();
  locationTimer.schedule(locationTask, 10000, cmds.getAlertInterval()*60);

and when .start is called, requestLocationUpdates () fails

+5
source share
2 answers

You need to call requestLocationUpdates from the stream using a looper, i.e. preferably the main stream. (requestLocationUpdates itself is quick and does not block, so it's not a shame to do this).

, , . , , : http://developer.android.com/resources/articles/timed-ui-updates.html

Runnable Activity.runOnUiThread() .

+4

, , LocationListener Threads ( Looper)/HandlerThreads. PendingIntent requestLocationUpdates (, minTime, minDistance, ). :

+1

All Articles