Error creating Thread inside a Handler

3

I need to give a delay of 5 seconds in one of the parts of my application:

final Runnable runnable = new Runnable() {
    @Override
    public void run() {
        // Encadeia trabalho serializado
        jobChain.append(new Job() {
            public void doJob(final OnChainItemListener itemListener) {
                // Obtém metadados do Artigo
                onlineArticle.get(idArticle, new HttpJsonObjectListener() {
                    public void onRequestCompleted(JSONObject object, Integer httpStatus, CharSequence msg) {
                        offlineArticle.save(object, new HttpJsonObjectListener() {
                            public void onRequestCompleted(JSONObject object, Integer httpStatus, CharSequence msg) {
                                Log.v(this.getClass().getName(), "Artigo salvo!");
                            }
                        }, null);

                        itemListener.onRequestCompleted(object, httpStatus, msg);
                    }
                }, defFail);
            }
        });

    }
};
new Handler().postDelayed(runnable, 5000);

I tried to implement, but it happens that all of this gets inside a handler and it gives me the following error:

  

java.lang.RuntimeException: Can not create handler inside thread that   has not called Looper.prepare ()

I read about it, it would have to run on the main thread, but I can not do it.

    
asked by anonymous 16.12.2015 / 21:23

1 answer

0

I decided to do the following:

private void createHandler() {
    Thread thread = new Thread() {
      public void run() {
           Looper.prepare();

           final Handler handler = new Handler();
           handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                   // Ação a ser atrasada
                    handler.removeCallbacks(this);
                    Looper.myLooper().quit();
               }
            }, 2000);

            Looper.loop();
        }
    };
    thread.start();
}
    
17.12.2015 / 17:30