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