asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0
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();
}
}
posted on 2011-07-01 17:37 汪杰 阅读(292) 评论(2)  编辑 收藏 引用 所属分类: Java

FeedBack:
# re: 简单模仿IntentService的实现
2011-09-19 13:16 | 汪杰
package com.asfman.test;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class TestService extends IntentService {
private boolean isRunning;
public TestService() {
super("TestService");
Log.i("info", "TestService");
}

@Override
protected void onHandleIntent(Intent intent) {
Log.i("info", "onHandleIntent");
// TODO Auto-generated method stub
for (long i = 0; i <= 5; i++) {
Log.i("info", " " + i);
try {
Thread.sleep(700);
} catch (InterruptedException e) {
}
}
if(isRunning) isRunning = false;
// stopSelf(-1);
startService(intent);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("info", "onStartCommand:" + startId);
Log.i("info", "isRunning:" + isRunning);
if(isRunning) {
return -1;
} else {
isRunning = true;
}

return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
Log.i("info", "onDestroy");
super.onDestroy();
isRunning = false;
}

@Override
public void onCreate() {
Log.i("info", "onCreate");
super.onCreate();
}

}
  回复  更多评论
  
# re: 简单模仿IntentService的实现
2011-10-14 16:41 | 汪杰
IntentService
@Override
public void onStart(Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}

startService(intent) 后执行onStart,通过mServiceHandler向HandlerThread的MessageQueue发送一条消息。然后HandlerThread中获取到后调用mServiceHandler.的handlerMessage进行处理,执行onHandleIntent,然后执行stopSelf(msg.arg1),stopSelf通过msg.arg1判断是否需要结束service  回复  更多评论
  
只有注册用户登录后才能发表评论。

<2019年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 384833
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜