package com.test;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
public class HandlerThread extends Service {
private ServiceHandler handler;
private Looper oLooper;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i("service", "onCreate:" + Thread.currentThread().toString());
MyThread fkThread = new MyThread();
fkThread.start();
oLooper = fkThread.getLooper();
handler = new ServiceHandler(oLooper);
}
@Override
public void onStart(Intent intent, int startId) {
Message msg = handler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
Log.i("service", "sendMessage:" + Thread.currentThread().toString());
handler.sendMessage(msg);
}
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
Log.i("service", "handleMessage:" + Thread.currentThread().toString());
//onHandleIntent((Intent)msg.obj);
stopSelf(msg.arg1);
}
}
class MyThread extends Thread {
private Looper oLooper;
public void run() {
Log.i("thread", "constructor");
Looper.prepare();
oLooper = Looper.myLooper();
Looper.loop();
}
public Looper getLooper() {
return oLooper;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
oLooper.quit();
}
}