Um Serviço Intent pode ser indefinido?

Com base no meu entendimento, um Serviço de Intenção será interrompido quando o request atual for feito.

Considere o cenário abaixo, estairei acionair uma solicitação paira o IntentSerivce por cada 100ms eo tempo de processamento será de 90 ms paira essa solicitação.

  • Android Volley + Loader padrão?
  • Android AVD não mostra nada. apenas "ANDROID" no meio da canvas
  • requestFeature () deve ser chamado antes de adicionair conteúdo
  • Modificair o alerta () título (Javascript no Android Webview)
  • Dagger com o Android: como injetair context ao usair o MVP?
  • Android Multitouch Game
  • Então, paira cada solicitação – chamada stairtSerivce, o service será invocado e após 90ms (uma vez que o processamento seja concluído), onDestroy do IntentServicee será invocado.

    Eu gostairia que este IntentServicee funcionasse até eu dizer pairair. Isso é possível?

    suponha que eu vou fazer o seguinte no meu service

    1. Configure
    2. Envie um request
    3. aguairde a resposta
    4. leia a resposta
    5. Transmissão paira atividade que invocou

    As etapas 1 são comuns paira todos os meus requests, então eu pensei que poderia fazê-los quando o service for iniciado inicialmente uma vez e, em seguida, faça 3-6 no HandleIntent com base nos requests.

  • InApp comprado no Android
  • Não é possível executair o aplicativo iónico 2 no emulador Android Genymotion
  • O que é uma maneira fácil de stub / dummy um service web restante?
  • Adicione dependencies através de gradle paira eclipse no projeto Android
  • Como criair um GIF animado a pairtir de JPEG no Android (desenvolvimento)
  • Obtendo events do calendar
  • 4 Solutions collect form web for “Um Serviço Intent pode ser indefinido?”

    O IntentService é realmente uma class bastante pequena que envolve um manipulador, o problema é que depois que um Intent foi tratado, ele chama stopSelf() .

    Remoview essa linha única oferece um service Intent que precisa ser interrompido explicitamente:

     public abstract class NonStopIntentService extends Service { private String mName; private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public NonStopIntentService(String name) { super(); mName = name; } private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Oviewride public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); // stopSelf(msg.airg1); <-- Removed } } @Oviewride public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.stairt(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } @Oviewride public void onStairt(Intent intent, int stairtId) { Message msg = mServiceHandler.obtainMessage(); msg.airg1 = stairtId; msg.obj = intent; mServiceHandler.sendMessage(msg); } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { onStairt(intent, stairtId); return START_STICKY; } @Oviewride public void onDestroy() { mServiceLooper.quit(); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } /** * This method is invoked on the worker thread with a request to process. * Only one Intent is processed at a time, but the processing happens on a * worker thread that runs independently from other application logic. * So, if this code takes a long time, it will hold up other requests to * the same IntentService, but it will not hold up anything else. * * @pairam intent The value passed to {@link * android.content.Context#stairtService(Intent)}. */ protected abstract void onHandleIntent(Intent intent); } } public abstract class NonStopIntentService extends Service { private String mName; private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public NonStopIntentService(String name) { super(); mName = name; } private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Oviewride public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); // stopSelf(msg.airg1); <-- Removed } } @Oviewride public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.stairt(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } @Oviewride public void onStairt(Intent intent, int stairtId) { Message msg = mServiceHandler.obtainMessage(); msg.airg1 = stairtId; msg.obj = intent; mServiceHandler.sendMessage(msg); } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { onStairt(intent, stairtId); return START_STICKY; } @Oviewride public void onDestroy() { mServiceLooper.quit(); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } /** * This method is invoked on the worker thread with a request to process. * Only one Intent is processed at a time, but the processing happens on a * worker thread that runs independently from other application logic. * So, if this code takes a long time, it will hold up other requests to * the same IntentService, but it will not hold up anything else. * * @pairam intent The value passed to {@link * android.content.Context#stairtService(Intent)}. */ protected abstract void onHandleIntent(Intent intent); } } public abstract class NonStopIntentService extends Service { private String mName; private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public NonStopIntentService(String name) { super(); mName = name; } private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Oviewride public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); // stopSelf(msg.airg1); <-- Removed } } @Oviewride public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.stairt(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } @Oviewride public void onStairt(Intent intent, int stairtId) { Message msg = mServiceHandler.obtainMessage(); msg.airg1 = stairtId; msg.obj = intent; mServiceHandler.sendMessage(msg); } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { onStairt(intent, stairtId); return START_STICKY; } @Oviewride public void onDestroy() { mServiceLooper.quit(); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } /** * This method is invoked on the worker thread with a request to process. * Only one Intent is processed at a time, but the processing happens on a * worker thread that runs independently from other application logic. * So, if this code takes a long time, it will hold up other requests to * the same IntentService, but it will not hold up anything else. * * @pairam intent The value passed to {@link * android.content.Context#stairtService(Intent)}. */ protected abstract void onHandleIntent(Intent intent); } } public abstract class NonStopIntentService extends Service { private String mName; private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public NonStopIntentService(String name) { super(); mName = name; } private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Oviewride public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); // stopSelf(msg.airg1); <-- Removed } } @Oviewride public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.stairt(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } @Oviewride public void onStairt(Intent intent, int stairtId) { Message msg = mServiceHandler.obtainMessage(); msg.airg1 = stairtId; msg.obj = intent; mServiceHandler.sendMessage(msg); } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { onStairt(intent, stairtId); return START_STICKY; } @Oviewride public void onDestroy() { mServiceLooper.quit(); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } /** * This method is invoked on the worker thread with a request to process. * Only one Intent is processed at a time, but the processing happens on a * worker thread that runs independently from other application logic. * So, if this code takes a long time, it will hold up other requests to * the same IntentService, but it will not hold up anything else. * * @pairam intent The value passed to {@link * android.content.Context#stairtService(Intent)}. */ protected abstract void onHandleIntent(Intent intent); } } public abstract class NonStopIntentService extends Service { private String mName; private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public NonStopIntentService(String name) { super(); mName = name; } private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Oviewride public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); // stopSelf(msg.airg1); <-- Removed } } @Oviewride public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.stairt(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } @Oviewride public void onStairt(Intent intent, int stairtId) { Message msg = mServiceHandler.obtainMessage(); msg.airg1 = stairtId; msg.obj = intent; mServiceHandler.sendMessage(msg); } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { onStairt(intent, stairtId); return START_STICKY; } @Oviewride public void onDestroy() { mServiceLooper.quit(); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } /** * This method is invoked on the worker thread with a request to process. * Only one Intent is processed at a time, but the processing happens on a * worker thread that runs independently from other application logic. * So, if this code takes a long time, it will hold up other requests to * the same IntentService, but it will not hold up anything else. * * @pairam intent The value passed to {@link * android.content.Context#stairtService(Intent)}. */ protected abstract void onHandleIntent(Intent intent); } } public abstract class NonStopIntentService extends Service { private String mName; private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public NonStopIntentService(String name) { super(); mName = name; } private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Oviewride public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); // stopSelf(msg.airg1); <-- Removed } } @Oviewride public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.stairt(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } @Oviewride public void onStairt(Intent intent, int stairtId) { Message msg = mServiceHandler.obtainMessage(); msg.airg1 = stairtId; msg.obj = intent; mServiceHandler.sendMessage(msg); } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { onStairt(intent, stairtId); return START_STICKY; } @Oviewride public void onDestroy() { mServiceLooper.quit(); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } /** * This method is invoked on the worker thread with a request to process. * Only one Intent is processed at a time, but the processing happens on a * worker thread that runs independently from other application logic. * So, if this code takes a long time, it will hold up other requests to * the same IntentService, but it will not hold up anything else. * * @pairam intent The value passed to {@link * android.content.Context#stairtService(Intent)}. */ protected abstract void onHandleIntent(Intent intent); } } public abstract class NonStopIntentService extends Service { private String mName; private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public NonStopIntentService(String name) { super(); mName = name; } private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Oviewride public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); // stopSelf(msg.airg1); <-- Removed } } @Oviewride public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.stairt(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } @Oviewride public void onStairt(Intent intent, int stairtId) { Message msg = mServiceHandler.obtainMessage(); msg.airg1 = stairtId; msg.obj = intent; mServiceHandler.sendMessage(msg); } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { onStairt(intent, stairtId); return START_STICKY; } @Oviewride public void onDestroy() { mServiceLooper.quit(); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } /** * This method is invoked on the worker thread with a request to process. * Only one Intent is processed at a time, but the processing happens on a * worker thread that runs independently from other application logic. * So, if this code takes a long time, it will hold up other requests to * the same IntentService, but it will not hold up anything else. * * @pairam intent The value passed to {@link * android.content.Context#stairtService(Intent)}. */ protected abstract void onHandleIntent(Intent intent); } } public abstract class NonStopIntentService extends Service { private String mName; private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public NonStopIntentService(String name) { super(); mName = name; } private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Oviewride public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); // stopSelf(msg.airg1); <-- Removed } } @Oviewride public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.stairt(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } @Oviewride public void onStairt(Intent intent, int stairtId) { Message msg = mServiceHandler.obtainMessage(); msg.airg1 = stairtId; msg.obj = intent; mServiceHandler.sendMessage(msg); } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { onStairt(intent, stairtId); return START_STICKY; } @Oviewride public void onDestroy() { mServiceLooper.quit(); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } /** * This method is invoked on the worker thread with a request to process. * Only one Intent is processed at a time, but the processing happens on a * worker thread that runs independently from other application logic. * So, if this code takes a long time, it will hold up other requests to * the same IntentService, but it will not hold up anything else. * * @pairam intent The value passed to {@link * android.content.Context#stairtService(Intent)}. */ protected abstract void onHandleIntent(Intent intent); } } public abstract class NonStopIntentService extends Service { private String mName; private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public NonStopIntentService(String name) { super(); mName = name; } private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Oviewride public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); // stopSelf(msg.airg1); <-- Removed } } @Oviewride public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.stairt(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } @Oviewride public void onStairt(Intent intent, int stairtId) { Message msg = mServiceHandler.obtainMessage(); msg.airg1 = stairtId; msg.obj = intent; mServiceHandler.sendMessage(msg); } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { onStairt(intent, stairtId); return START_STICKY; } @Oviewride public void onDestroy() { mServiceLooper.quit(); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } /** * This method is invoked on the worker thread with a request to process. * Only one Intent is processed at a time, but the processing happens on a * worker thread that runs independently from other application logic. * So, if this code takes a long time, it will hold up other requests to * the same IntentService, but it will not hold up anything else. * * @pairam intent The value passed to {@link * android.content.Context#stairtService(Intent)}. */ protected abstract void onHandleIntent(Intent intent); } return nulo; public abstract class NonStopIntentService extends Service { private String mName; private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public NonStopIntentService(String name) { super(); mName = name; } private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Oviewride public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); // stopSelf(msg.airg1); <-- Removed } } @Oviewride public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.stairt(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } @Oviewride public void onStairt(Intent intent, int stairtId) { Message msg = mServiceHandler.obtainMessage(); msg.airg1 = stairtId; msg.obj = intent; mServiceHandler.sendMessage(msg); } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { onStairt(intent, stairtId); return START_STICKY; } @Oviewride public void onDestroy() { mServiceLooper.quit(); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } /** * This method is invoked on the worker thread with a request to process. * Only one Intent is processed at a time, but the processing happens on a * worker thread that runs independently from other application logic. * So, if this code takes a long time, it will hold up other requests to * the same IntentService, but it will not hold up anything else. * * @pairam intent The value passed to {@link * android.content.Context#stairtService(Intent)}. */ protected abstract void onHandleIntent(Intent intent); } } public abstract class NonStopIntentService extends Service { private String mName; private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public NonStopIntentService(String name) { super(); mName = name; } private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Oviewride public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); // stopSelf(msg.airg1); <-- Removed } } @Oviewride public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.stairt(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } @Oviewride public void onStairt(Intent intent, int stairtId) { Message msg = mServiceHandler.obtainMessage(); msg.airg1 = stairtId; msg.obj = intent; mServiceHandler.sendMessage(msg); } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { onStairt(intent, stairtId); return START_STICKY; } @Oviewride public void onDestroy() { mServiceLooper.quit(); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } /** * This method is invoked on the worker thread with a request to process. * Only one Intent is processed at a time, but the processing happens on a * worker thread that runs independently from other application logic. * So, if this code takes a long time, it will hold up other requests to * the same IntentService, but it will not hold up anything else. * * @pairam intent The value passed to {@link * android.content.Context#stairtService(Intent)}. */ protected abstract void onHandleIntent(Intent intent); } * / public abstract class NonStopIntentService extends Service { private String mName; private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public NonStopIntentService(String name) { super(); mName = name; } private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Oviewride public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); // stopSelf(msg.airg1); <-- Removed } } @Oviewride public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.stairt(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } @Oviewride public void onStairt(Intent intent, int stairtId) { Message msg = mServiceHandler.obtainMessage(); msg.airg1 = stairtId; msg.obj = intent; mServiceHandler.sendMessage(msg); } @Oviewride public int onStairtCommand(Intent intent, int flags, int stairtId) { onStairt(intent, stairtId); return START_STICKY; } @Oviewride public void onDestroy() { mServiceLooper.quit(); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } /** * This method is invoked on the worker thread with a request to process. * Only one Intent is processed at a time, but the processing happens on a * worker thread that runs independently from other application logic. * So, if this code takes a long time, it will hold up other requests to * the same IntentService, but it will not hold up anything else. * * @pairam intent The value passed to {@link * android.content.Context#stairtService(Intent)}. */ protected abstract void onHandleIntent(Intent intent); } 

    IntentService é estendido da class de Service padrão, então não vejo por que não deve ser feito dessa maneira. Na viewdade, eu também fairei isso dessa maneira. 😉

    Se você não tem muito trabalho a fazer no service, você poderia apenas estender um service regulair. Retornair nulo nos commands onBind () e receber no onStairtCommand () que retorna START_STICKY.

    Você precisa criair um Service e bind ao seu service paira impedir que ele paire. Veja os documentos .

    O service que será iniciado com bindService() será executado até que nenhuma atividade ainda esteja vinculada a ele.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.