【发布时间】:2010-08-23 23:48:47
【问题描述】:
我一直想知道是否可以使用IntentService 进行一些联网,同时保持待处理意图队列的优先级。我的目标是能够在后台下载一些图像,在需要时添加更多图像(发送另一个Intent)并在必要时能够重置队列(最好使用特定的 Intent)。 IntentServie 可以做到这一切,但是当我发送“停止”Intent 时,它需要作为队列中的下一个项目进行处理,而不是现在的最后一个。
编辑
对于那些感兴趣的人,我已经将IntentService 的 AOSP 代码修改为满足我的需要。我不能只继承 IntentHandler 的原因是因为 IntentHandler 内部的私有 ServiceHandler 类。
ServiceHandler里面我有一个新方法:
public final boolean sendPriorityMessage(Message msg)
{
int priority = msg.arg2;
//Log.i(GenericList.TAG,"recieved message priority: "+priority);
if(priority>PRIORITY_NORMAL){
return sendMessageAtFrontOfQueue(msg);
}else{
return sendMessage(msg);
}
}
从onStart 调用此方法,而不仅仅是sendMessage
@Override
public void onStart(Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
try{
msg.arg2 = intent.getExtras().getInt(KEY_PRIORITY);
}catch(Exception e){
msg.arg2 = PRIORITY_NORMAL;
}
mServiceHandler.sendPriorityMessage(msg);
}
总体而言,代码仍然有限,但我能够将一些消息快速跟踪到队列的前面,这正是我所追求的。
【问题讨论】:
标签: java android service android-intent priority-queue