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

  • Home
  • SEARCH
  • 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 8202501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:12:27+00:00 2026-06-07T07:12:27+00:00

I’m developing an Android 3.1 Tablet application. This app will run a thread. On

  • 0

I’m developing an Android 3.1 Tablet application.

This app will run a thread. On its service I have this code:

public class UDPSocketBackgroundService extends Service
{

@Override
public void onCreate()
{
    super.onCreate();
    Log.v(TAG, "in onCreate()");

    // xxxxxx
    Looper.prepare();
    mServiceHandler = new Handler() {
        /**
         * 
         */
        public void handleMessage(Message msg)
        {
            DatagramPacket packet = (DatagramPacket) msg.obj;
            //String received = new String(packet.getData(), 0, packet.getLength());

            Message backMsg = Message.obtain();
            backMsg.arg1 = Activity.RESULT_OK;;

            Bundle bundle = new Bundle();
            bundle.putByteArray("CLIENT_DATA", packet.getData());
            bundle.putString("CLIENT_HOST", packet.getAddress().getHostAddress());
            bundle.putString("CLIENT_PORT", new Integer(packet.getPort()).toString());
            backMsg.setData(bundle);

            try
            {
                outMessenger.send(backMsg);
            }
            catch (android.os.RemoteException e1)
            {
                Log.w(getClass().getName(), "Exception sending message", e1);
            }
        }
    };
    Looper.loop();
}

But I don’t know why (because it is my first time with services) I get an exception here:

Looper.prepare();

UPDATE

Service’s onBind event:

public IBinder onBind(Intent intent)
{
    Bundle extras = intent.getExtras();
    // Get messager from the Activity
    if (extras != null) {
        outMessenger = (Messenger) extras.get("MESSENGER");
    }

    try
    {
        myThread = new UDPServerThread("X", 8888, mServiceHandler);
        myThread.start();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return mMessenger.getBinder();
}

This how I start the service from main activity:

protected void onResume()
{
    super.onResume();

    Intent intent = null;
    intent = new Intent(this, UDPSocketBackgroundService.class);
    // Create a new Messenger for the communication back
    // From the Service to the Activity
    Messenger messenger = new Messenger(handler);
    intent.putExtra("MESSENGER", messenger);

    bindService(intent, conn, Context.BIND_AUTO_CREATE);
}

What am I doing wrong?

  • 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-07T07:12:29+00:00Added an answer on June 7, 2026 at 7:12 am

    1. Service doesn’t run on Background thread, but on the Dedicated UI thread.

    2. It was considered good practice to have UI work on UI thread, and Non-UI work on Non-UI thread, but from HoneyComb it became a rule.

    3. So for your background thread use Thread will Handler, or AsyncTask.

    4. Non-UI work are executed on the foreground using Handler, which in turn requires UI thread to call Looper.loop() to get the actual messages serviced, So there is no need to explicitly call Looper.loop() or prepare().

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

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.