【问题标题】:Service with priority queue in AndroidAndroid中具有优先级队列的服务
【发布时间】:2011-05-26 15:18:57
【问题描述】:

我想实现一个IntentService (source code),它基于一个简单的数字优先级来处理意图。服务应首先处理优先级较高的 Intent,而不是优先级较低的 Intent。

Android 上是否有任何东西可以做到这一点?如果没有,有什么具体的实现方法吗?

【问题讨论】:

    标签: android service queue android-intent priority-queue


    【解决方案1】:

    根据 CommonsWare 的回答和 Android 的 IntentServicesource code,首次尝试实现具有优先级的意图服务。将进行广泛的测试并进行相应的编辑。

    public abstract class PriorityIntentService extends Service {
    
        private final class QueueItem implements Comparable<QueueItem> {
            Intent intent;
            int priority;
            int startId;
    
            @Override
            public int compareTo(QueueItem another) {
                if (this.priority < another.priority) {
                    return -1;
                } else if (this.priority > another.priority) {
                    return 1;
                } else {
                    return (this.startId < another.startId) ? -1 : 1;
                }
            }
        }
        private final class ServiceHandler extends Handler {
            public ServiceHandler(Looper looper) {
                super(looper);
            }
    
            @Override
            public void handleMessage(Message msg) {
                try {
                    final QueueItem item = mQueue.take();
                    onHandleIntent(item.intent);
                    if (mQueue.isEmpty()) {
                        PriorityIntentService.this.stopSelf();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        public static final String EXTRA_PRIORITY = "priority";
        private String mName;
        private PriorityBlockingQueue<QueueItem> mQueue;
        private boolean mRedelivery;
        private volatile ServiceHandler mServiceHandler;
        private volatile Looper mServiceLooper;
    
        public PriorityIntentService(String name) {
            super();
            mName = name;
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            HandlerThread thread = new HandlerThread("PriorityIntentService[" + mName + "]");
            thread.start();
    
            mServiceLooper = thread.getLooper();
            mServiceHandler = new ServiceHandler(mServiceLooper);
            mQueue = new PriorityBlockingQueue<QueueItem>();
        }
    
        @Override
        public void onDestroy() {
            mServiceLooper.quit();
        }
    
        protected abstract void onHandleIntent(Intent intent);
    
        @Override
        public void onStart(Intent intent, int startId) {
            final QueueItem item = new QueueItem();
            item.intent = intent;
            item.startId = startId;
            final int priority = intent.getIntExtra(EXTRA_PRIORITY, 0);
            item.priority = priority;
            mQueue.add(item);
            mServiceHandler.sendEmptyMessage(0);
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            onStart(intent, startId);
            return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
        }
    
        public void setIntentRedelivery(boolean enabled) {
            mRedelivery = enabled;
        }
    }
    

    【讨论】:

      【解决方案2】:

      不是真的。不过,IntentService 的内容并不多。制作由PriorityBlockingQueue 支持的PriorityIntentService,而不是Handler+Looper,应该不会太久。

      【讨论】:

      • 感谢 CommonsWare。你介意添加一点伪代码来为我指明正确的方向吗?我猜 PriorityBlockingQueue 应该存储意图,比较器应该区分不同的优先级。不过,不确定如何对具有相同优先级的意图进行排序。
      • @hgpc:如果你没有其他条件,比较哈希码什么的。
      • @CommonsWare 意图是否有某种时间戳?
      • @hgpc:我不知道。不过,你可以多放一个。我假设这就是您要优先考虑的地方。
      • @CommonsWare 是的。被问到以防万一我可以为此重复使用。
      猜你喜欢
      • 2023-04-02
      • 1970-01-01
      • 2011-01-18
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多