能否在IntentService无限期地运行?IntentService

由网友(意中人是位盖世垃圾)分享简介:根据我的理解,一个IntentService将获得当它的当前请求完成后停止。Based on my understanding, an IntentService will get stopped when its current request is done.请考虑以下的情况下,我会触发一个请求到IntentS...

根据我的理解,一个IntentService将获得当它的当前请求完成后停止。

Based on my understanding, an IntentService will get stopped when its current request is done.

请考虑以下的情况下,我会触发一个请求到IntentSerivce为每100ms和处理时间将是90毫秒的要求。

Consider the below scenario, i will be triggering a request to the IntentSerivce for every 100ms and the processing time will be 90 ms for that request.

因此​​,对于每一个我的要求 - startSerivce电话,服务将被调用后90毫秒(一旦处理完成),该IntentServicee中的onDestroy将被调用

So for every my request - startSerivce call, service will get invoked and after 90ms (once the processing is done), onDestroy of the IntentServicee will get invoked.

我想有这个IntentServicee运行,直到我说停止。这可能吗?

I would like to have this IntentServicee running till i say stop. Is that possible?

假设我打算做低于我的服务

assume that i am going to do the below in my service

请配置 发送一些要求 在等待回应 在读取响应 广播来的活动,调用

第1步是适用于所有我的要求,所以我想我可以做他们当服务已启动最初一次,根据请求的HandleIntent然后做3-6。

Steps 1 is common for all my requests, so i thought i can do them when service is started initially once and then do 3-6 in HandleIntent based on the requests.

推荐答案

IntentService 实际上是pretty的小班它包装一个处理程序,问题在于一个Intent已处理后,它会调用 stopSelf()

The IntentService is actually a pretty small class which wraps a Handler, the problem being that after an Intent has been handled it calls stopSelf().

删除了一行给你一个IntentService需要被明确停止:

Removing that single line gives you an IntentService which needs to be explicitly stopped:

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);  

}
阅读全文

相关推荐

最新文章