VansFannel
VansFannel

Reputation: 45941

Unable to create service "Only one Looper may be created per thread"

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?

Upvotes: 0

Views: 1158

Answers (3)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

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().

Upvotes: 3

Adam Monos
Adam Monos

Reputation: 4307

First, Service doesn't run on a background thread so you should use either a Thread, an AsyncTask or an IntentService for your background work. Second, Service already has a Looper initialized, you have to remove Looper.prepare() and Looper.loop() from your code.

Upvotes: 1

Ian Warwick
Ian Warwick

Reputation: 4784

A service runs on the UI thread and a the UI thread already has a looper which is why you are getting that error.

If you intented to do your work in a background thread then you might need to create a new thread for that code you want to run.

Maybe you could look into HandlerThread.

Upvotes: 1

Related Questions