【问题标题】:Can an IntentService run indefinitely?IntentService 可以无限期运行吗?
【发布时间】:2011-07-27 08:05:11
【问题描述】:

根据我的理解,当当前请求完成时,IntentService 将停止。

考虑以下场景,我将每 100 毫秒触发一次对 IntentSerivce 的请求,该请求的处理时间为 90 毫秒。

因此,对于我的每个请求 - startSerivce 调用,都会调用服务,并在 90 毫秒后(一旦处理完成),将调用 IntentServicee 的 onDestroy。

我想让这个 IntentServicee 一直运行到我说停止为止。这可能吗?

假设我将在我的服务中执行以下操作

  1. 进行配置
  2. 发送一些请求
  3. 等待回复
  4. 阅读回复
  5. 广播到调用的活动

步骤 1 对我的所有请求都是通用的,所以我认为我可以在服务最初启动一次时执行它们,然后根据请求在 HandleIntent 中执行 3-6。

【问题讨论】:

    标签: android intentservice


    【解决方案1】:

    IntentService 实际上是一个非常小的类,它包装了一个处理程序,问题是在处理了一个 Intent 之后它会调用stopSelf()

    删除该单行会为您提供一个需要明确停止的 IntentService:

    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);
            }
    
            @Override
            public void handleMessage(Message msg) {
                onHandleIntent((Intent)msg.obj);
                // stopSelf(msg.arg1); <-- Removed
            }
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
            thread.start();
    
            mServiceLooper = thread.getLooper();
            mServiceHandler = new ServiceHandler(mServiceLooper);
        }
    
        @Override
        public void onStart(Intent intent, int startId) {
            Message msg = mServiceHandler.obtainMessage();
            msg.arg1 = startId;
            msg.obj = intent;
            mServiceHandler.sendMessage(msg);
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            onStart(intent, startId);
            return START_STICKY;
        }    
    
        @Override
        public void onDestroy() {
            mServiceLooper.quit();
        }
    
        @Override
        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.
         *
         * @param intent The value passed to {@link
         *               android.content.Context#startService(Intent)}.
         */
        protected abstract void onHandleIntent(Intent intent);  
    
    }
    

    【讨论】:

      【解决方案2】:

      IntentService 是从标准 Service 类扩展而来的,所以我不明白为什么不应该这样做。事实上,我也会这样做。 ;)

      【讨论】:

        【解决方案3】:

        如果您在服务中没有太多工作要做,您可以延长常规服务。在 onBind() 中返回 null 并在返回 START_STICKY 的 onStartCommand() 中接收命令。

        【讨论】:

          【解决方案4】:

          您需要为您的服务创建一个Servicebind 以防止它停止。请参阅docs

          将以bindService() 启动的服务将一直运行,直到没有 Activity 仍然绑定到它为止。

          【讨论】:

          • 你的意思是bindService不能用IntentService做吗?我需要扩展服务而不是 IntentService 吗?
          • 我不确定 IntentService.. 但我认为它也可以工作。不过我从来没有尝试过。
          • 我能够将活动与 IntentService 绑定。它似乎工作正常。当我说解除绑定时,IntentService 的 OnDestroy 被调用。但我只是想确保在 Android 中是否允许这样做(绑定到 IntentService)?你有任何与此相关的信息吗?非常感谢您的意见。
          猜你喜欢
          • 1970-01-01
          • 2023-02-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-08
          相关资源
          最近更新 更多