使用 IntentService 进行优先网络

发布于 2024-09-16 04:19:15 字数 1407 浏览 5 评论 0原文

我一直想知道是否可以使用 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);
    }

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

I've been wondering if it is possible to use an IntentService to do some networking while keeping the queue of pending intents prioritized. My goal to be able to download some images in the background, add more if needed (send another Intent) and be able to reset the queue if necessary (preferably using a specific Intent). That is all possible with an IntentServie but when I send that 'stop' Intent it needs to be processed as the next item in the queue, not the last where it is right now.

EDIT

For those interested I have taken the AOSP code for IntentService and modified it to meet my needs. The reason I cannot just subclass IntentHandler is because of the private ServiceHandler class inside of IntentHandler.

Inside of the ServiceHandler I have a new method:

 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);    
            }

        }

This method is called from onStart instead of just 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);
    }

Overall the code is still limited but I am able to fast track some messages to the front of the queue, which is what I was after anyway.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衬遮猫 2024-09-23 04:19:15

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

you could implement/extend your own PriorityQueue that simply checks every new intent added to the queue. if it's the stop intent, it moves it straight to the front of the line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文