0

Possible Duplicate:
Can’t create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog

I'm developing an Android service that try to obtain the device IP address every x time and comunicate it to a server. I'm using:

Netbeans 7.2
Android SDK
Android Google-Api 8
SQLite

I know there are a few questions related to this same issue, but none of them gave me a solution to my problem. As you can see in my code below, I'm not trying to access to the UI of the service main thread (well, I tried, but after I commented the line, the error remains the same). On the other hand, I'm using AsyncTask, which I think is the appropriate way to do it.

This is the main part of my service:

public class ArsosService extends Service {

    private NotificationManager mNM;
        private final Messenger mMessenger = new Messenger(new IncomingHandler());
        protected DatabaseUtil dbu = null;
        @Override
        public void onCreate() {
            mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            try {
                dbu = DatabaseUtility.getInstance(this);
            } catch (IOException ex) {
                Log.e("Service", ex);
            }
            Timer timer = new Timer();
            timer.schedule(new Checks(), 0, 15000);
        }

        private class Checks extends TimerTask {
            @Override
            public void run() {
                CheckIpAddress_Task checkIp = new CheckIpAddress_Task();         
                checkIp.execute();
            }
        }

        // Other methods

        private class CheckIpAddress_Task extends AsyncTask<Void, Void, Integer> {
        @Override
        protected Integer doInBackground(Void... arg0) {
            String ipLocal = getLocalIpAddress();
            String text = null;

            // ipLocal==null means there is no available connection, so we do nothing. 
            if (ipLocal != null) {
                String ipDb = dbu.getLastIP(); // we get the IP saved in the DB.
                if (ipDb == null) {
                    dbu.addProperty("IP", ipLocal); // we save the IP in the DB.
                } else if (!ipLocal.equals(ipDb)) {
                    dbu.setProperty("IP", ipLocal); // we update the IP in the DB.
                }
            }
            if (text != null) {
                //showNotification(1, text, ipLocal);
            }
            return 0;
        }

        private String getLocalIpAddress() {
            String result = null;
            // Irrelevant code
            return result;
        }
    }
}

I think the problem may be related to the threads, but I can't see where. Any help will be appreciated.

EDITED: Although I have accepted one of the answers as correct, or maybe because of it, I've been searching for some more information regard to it. I've run into this page I want to share with all of you who someday need to know more about this issue. Its author,Tejas Lagvankar, explains everything about threads, loopers and handler in a very clear and understandable way.

Community
  • 1
  • 1
Julio
  • 446
  • 1
  • 7
  • 22

2 Answers2

1

Try this...

- First declare the Handler Object reference variable at class scope.

Handler h;

- Inside the onCreate() method create the instance of the Handler.

h = new Handler();

- Use it with thread like below:

new Thread(new Runnable(){

 public void run(){

    h.post(new Runnable(){


      // Do the UI work here.

   });

 }


});

- You can very well use the AsyncTask, provided in android, its known as P*ainless threading.*

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • First of all, my apologies for not answering before. Thank you for your time and your answer. I followed your indications and it works great. However, I'm not very sure if this is a very orthodox way to do it. My method now looks like this: private class Checks extends TimerTask { @Override public void run() { h.post(new Runnable() { public void run() { CheckIpAddress_Task checkIp = new CheckIpAddress_Task(); checkIp.execute(); } }); } } As I said, this wordk, but is it correct? – Julio Oct 11 '12 at 19:09
1

Handler always runs in the Looper thread context. When you declare a seperate thread, its context is different from the Looper. Hence the error.

Simple solution is always declare Handlers in onCreate(), onStart() and onResume(). If you use AsyncTasks, you can very well declare handlers in onPreExecute() and onPostExecute() as they too run in the Looper context.

Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
  • Thank you for your answer. With this information and Kumar Vivek Mitra's, I got my problem fixed. – Julio Oct 11 '12 at 19:17