2010-12-16 20 views
5

मैं IntentService (source code) को लागू करना चाहता हूं जो एक साधारण संख्यात्मक प्राथमिकता के आधार पर इरादों को संभालता है। उच्च प्राथमिकता वाले इरादे को कम प्राथमिकता वाले इरादों की तुलना में पहले सेवा द्वारा संसाधित किया जाना चाहिए।एंड्रॉइड में प्राथमिकता कतार के साथ सेवा

क्या एंड्रॉइड पर कुछ भी है जो पहले से ही करता है? यदि नहीं, तो इसे कैसे कार्यान्वित किया जाए इस पर कोई संकेतक?

उत्तर

18

कॉमन्सवेयर के उत्तर और एंड्रॉइड के 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 के लिए बहुत कुछ नहीं है। + Looper की बजाय PriorityBlockingQueue द्वारा समर्थित PriorityIntentService को तैयार करना, बहुत लंबा नहीं होना चाहिए।

+0

धन्यवाद कॉमन्सवेयर। क्या आप मुझे सही दिशा में इंगित करने के लिए छद्म कोड का थोड़ा सा जोड़ना चाहते हैं? मुझे लगता है कि प्राथमिकताब्लॉकिंग क्यूई को इरादों को स्टोर करना चाहिए, और तुलनित्र को विभिन्न प्राथमिकताओं के बीच अंतर करना चाहिए। हालांकि, वही प्राथमिकता के इरादे को आदेश देने का तरीका सुनिश्चित नहीं है। – hpique

+0

@hgpc: यदि आपके पास कोई अन्य मानदंड नहीं है, तो हैश कोड या कुछ की तुलना करें। – CommonsWare

+0

@ कॉमन्सवेयर क्या किसी इरादे का कोई टाइमस्टैम्प है? – hpique

संबंधित मुद्दे