【问题标题】:Using an IntentService to do Prioritized Networking使用 IntentService 进行优先网络连接
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • PriorityQueue 本身只是数据结构,我需要的是使用 PriorityQueue 来存储待处理意图的 IntentService。如果我要继承并覆盖相应的方法,这可能是可能的。是时候挖掘一些 Android 的源代码了……
    猜你喜欢
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 2013-02-17
    相关资源
    最近更新 更多