Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8671949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:04:51+00:00 2026-06-12T19:04:51+00:00

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

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:04:52+00:00Added an answer on June 12, 2026 at 7:04 pm

    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.*

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask
Possible Duplicate: How can I remove the event handler once the click event has
Possible Duplicate: How I can create installer for website. PHP mysql I'm looking to
Possible Duplicate: Create shortcut to console.log() In javascript we can easy assign functions to
Possible Duplicate: Can we use any other TAG inside <ul> along with <li>? Or
Possible Duplicate: Can not connect to Oracle via VBA - Driver's SQLSetConnectAttr Failed I
Possible Duplicate: Can I create an automatic property (no private member) with get and
Possible Duplicate: How can I create a multilingual android application? I am creating application
Possible Duplicate: Why can’t I create an abstract constructor on an abstract C# class?
Possible Duplicate: declare property as object? in java you can create an object directly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.