在Android中使用优先级队列的服务

hpi*_*que 5 queue service android priority-queue android-intent

我想实现一个基于简单数字优先级处理意图的IntentService(源代码).具有较高优先级的意图应首先由服务处理,而不是优先级较低的意图.

Android上有没有这样做呢?如果没有,关于如何实现它的任何指针?

hpi*_*que 18

基于CommonsWare的答案和Android的IntentService 源代码,首次尝试实现具有优先级的意图服务.将进行广泛测试并进行相应编辑.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)