gpt4 book ai didi

java - 使用 IntentService 进行优先网络

转载 作者:行者123 更新时间:2023-12-01 05:54:25 26 4
gpt4 key购买 nike

我一直想知道是否可以使用 IntentService 进行一些网络操作,同时保持待处理 Intent 队列的优先级。我的目标是能够在后台下载一些图像,根据需要添加更多图像(发送另一个 Intent),并能够在必要时重置队列(最好使用特定的 Intent)。这一切都可以通过 IntentServie 实现,但是当我发送“停止”Intent 时,它需要作为队列中的下一个项目进行处理,而不是正确的最后一个项目现在。

编辑

对于那些感兴趣的人,我已经获取了 IntentService 的 AOSP 代码并对其进行了修改以满足我的需求。我不能只是子类化 IntentHandler 的原因是因为 IntentHandler 内部有私有(private) 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);
}

总的来说,代码仍然有限,但我能够将一些消息快速跟踪到队列的前面,这正是我所追求的。

最佳答案

您可以实现/扩展您自己的 PriorityQueue,它只需检查添加到队列中的每个新 Intent 。如果是停止 Intent ,则会将其直接移动到该行的前面。

关于java - 使用 IntentService 进行优先网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3552549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com