Can't create handler inside thread that has not called Looper.prepare() in CountDownTimer

Leonard Febrianto picture Leonard Febrianto · Aug 29, 2015 · Viewed 10.2k times · Source

I have a service. And there is a method called onServiceUpdate(). This method is similiar with onLocationChanged() in Google Maps API.

So i want to start CountDownTimer inside onServiceUpdate() method but showing error like this :

Can't create handler inside thread that has not called Looper.prepare()
    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
            at android.os.Handler.<init>(Handler.java:200)
            at android.os.Handler.<init>(Handler.java:114)
            at android.os.CountDownTimer$1.<init>(CountDownTimer.java:114)
            at android.os.CountDownTimer.<init>(CountDownTimer.java:114)
            at skripsi.ubm.studenttracking.Service2$6.<init>(Service2.java:317)
            at skripsi.ubm.studenttracking.Service2.onServiceUpdate(Service2.java:317)

This is my code :

    @Override
    public void onServiceUpdate(ServiceState state)
    {
final float[] distance = new float[2];
        Location.distanceBetween(state.getGeoPoint().getLatitude(), state.getGeoPoint().getLongitude(), 6.130607787619352,106.81839518499267, distance);
        if (distance[0] > 25.0)
        {

                        CountDownTimer cdt5  = new CountDownTimer(total_onServiceUpdate,1000) {
                            @Override
                            public void onTick(long millisUntilFinished) {
                                total_onServiceUpdate = millisUntilFinished/1000;
                            }

                            @Override
                            public void onFinish() {
                                sendSMS();
                                stopSelf();
                            }
                        }.start();

        }

Answer

Elltz picture Elltz · Sep 1, 2015

the onServiceUpdate() is an aysnchronous task that runs and notifies you, hence its a background thread. all you need to do is call timer.start(); from the main Thread, the Service actually runs on the main Thread, it is intentService that doesn't so, your solution is along the ways of

new Handler(Looper.getMainLooper()).post(new Runnable() {           
        @Override
        public void run() {
            CountDownTimer cdt5  = new CountDownTimer(total_onServiceUpdate,1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            total_onServiceUpdate = millisUntilFinished/1000;
                        }

                        @Override
                        public void onFinish() {
                            sendSMS();
                            stopSelf();
                        }
                    }.start();
        }
    });

now you can continue sir. Always call codes the flirt with the Screen on the main Looper

Hope it helps