Servicio con cola de prioridad en Android
Me gustaría implementar un IntentService ( código fuente ) que maneja intentos basados en una simple prioridad numérica. Los intentos con mayor prioridad deben ser procesados primero por el servicio que los intentos con menor prioridad.
¿Hay algo en Android que haga esto ya? Si no, ¿hay indicaciones sobre cómo implementarlo?
- El servicio de fondo de Android se está reiniciando cuando se cancela la aplicación
- Facturación en aplicaciones de Android y servicio bindService
- Android startService () tarda mucho tiempo en volver al subproceso de interfaz de usuario
- El servicio de Android solo se inicia en modo de depuración
- Android Development: Cambio del brillo de la pantalla en el servicio
- MVC en Android: ¿Aplicación o servicio para actualizaciones asíncronas?
- Android - ¿Cómo decidir si ejecutar un servicio en un proceso separado?
- ¿Por qué NullPointerException ocurre con el uso de BillingService estándar?
- Servicio de Android - URL de ping
- ¿Es posible ejecutar mi servicio por tiempo indefinido?
- ¿Hay una alternativa onBackPressed () para un servicio?
- ¿Cómo saber cuántas aplicaciones se ejecutan en segundo plano en android?
- Diferencia entre los receptores de servicio y difusión en android
Un primer intento de implementación de un servicio de intenciones con prioridades, basado en la respuesta de IntentService
y el código fuente de IntentService
de Android. Se probará ampliamente y editar en consecuencia.
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; } }
Realmente no. Sin IntentService
, no hay mucho para IntentService
. Cocinar un PriorityIntentService
respaldado por un PriorityBlockingQueue
, en lugar de un Handler
+ Looper
, no debe ser mucho más largo.
- Cómo saber si el teléfono está en modo inactivo / inactivo para Android
- View switches: cómo bloquear la interacción del usuario en el medio?