IntentService:如何正确地排队? [英] IntentService : How to enqueue correctly?

查看:143
本文介绍了IntentService:如何正确地排队?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的code我使用的是 IntentService 听位置更新(GPS或网络更新),这 IntentService 被触发接收到事件的时候,所以它开始与 startService()的任何活动。

In my code i'm using an IntentService to listen to location updates (either GPS or network updates) and this IntentService is triggered when an event is received, so it is started with startService() from any activity.

public class AddLocationService extends IntentService implements LocationListener {
    /*My code here*/
}

    @Override
protected void onHandleIntent(Intent intent) {
    if(getOldLoc() == null) 
    { 
        //Get a new location
        this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_INTERVAL_GPS, 0, this);
        this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_INTERVAL_GPS, 0, this);
        Log.d(AddLocationService.TAG, "Network listener started");

        this.time_start_listening = System.currentTimeMillis();
        mTimerThread mTimerRunnable = new mTimerThread();
        this.timerThread = new Thread(mTimerRunnable);
        this.timerThread.start();
    }
    else    
        /*REUSE OLD LOCATION*/
}

现在我的问题是:当两个事件启动该 IntentService 和第二启动它,而第一个仍要求更新,我会想第二个等待直到第一种是完全完成(地点发现或定时器线程完成)。 但是每当 IntentService 执行第二次(第一个实例仍然在运行),它打印我的日志,并不会因为它是并行执行。

Now my problem is : When two events start this IntentService and the second starts it while the first one is still requesting for updates, I will like the second one to wait until first one is fully finished (location found OR timer thread finishes). However whenever the IntentService is executed a second time (first instance still running), it prints me the log and does as it was executing in parallel.

不过,我认为 IntentService 的主要目标是,它是连续的东西所以第二个意图将不得不等待,直到第一个完成...

However I thought that the main goal of IntentService was that it is something sequential so a second intent would have to wait until first one is done...

难道我missunderstood什么?

Did I missunderstood something ?

推荐答案

似乎你onHandleIntent方法不阻断其上执行线程,因此它会快速返回,并允许所述第二意图进行处理。不仅如此,但是从LocationManager任何回调到该线程不太可能被处理为后台线程可能在onHandleIntent完成被杀死。

It appears that your onHandleIntent method is not blocking the thread it is executing on, so it will return quickly and allow the second intent to be processed. Not only that, but any callbacks from the LocationManager to that thread are unlikely to be processed as the background thread is likely to be killed when onHandleIntent is finished.

如果你真的想用IntentService来管理你的意图的队列,那么你将需要做你的位置处理自己的线程,并加入IntentService线的位置线,而它在等待位置的回调。

If you really want to use IntentService to manage your intent queue then you will need to do your location handling on its own thread, and join the IntentService thread to the location thread whilst it is waiting for the location callback.

下面有一点点code演示的想法:

Heres a bit of code that demonstrates the idea:

public class TestService extends IntentService {
    private static final String TAG = "TestService";

    private Location mLocation = null;

    public TestService() {
       super(TAG);
    }

    @Override
    public void onHandleIntent(Intent intent) {
        Log.d(TAG, "onHandleIntent");

        if (mLocation == null) {
            Log.d(TAG, "launching location thread");
            LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

            LocationThread thread = new LocationThread(locationManager);
            thread.start();
            try {
                thread.join(10000);
            } catch (InterruptedException e) {
                Log.d(TAG, "timeout");
                return;
            }

            Log.d(TAG, "join finished, loc="+mLocation.toString());
        } else {
             Log.d(TAG, "using existing loc="+mLocation.toString());
        }
    }

    private class LocationThread extends Thread implements LocationListener  {
        private LocationManager locationManager = null;

        public LocationThread(LocationManager locationManager) {
            super("UploaderService-Uploader");
            this.locationManager = locationManager;
        }

        @Override
        public void run() {
            Log.d(TAG, "Thread.run");
            Looper.prepare();
           this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
            this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

            Looper.loop();
        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            Log.d(TAG, "onLocationChanged("+location.toString()+")");
            mLocation = location;
            Looper.myLooper().quit();
         }

         @Override
         public void onProviderDisabled(String arg0) {
         }

         @Override
         public void onProviderEnabled(String arg0) {
         }

         @Override
         public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
         }

   }
}

有趣的是在那里是运行一个消息循环的线程上的活套(允许​​处理的回调)。

Of interest in there is the Looper that runs a message loop on the thread (to allow handling of the callbacks).

由于与IntentService为此所需付出的努力可能是值得研究的衍生服务,而不是和管理自己的意图的队列。

Given the effort required to do this with IntentService it might be worthwhile investigating deriving from Service instead and managing your own intent queue.

这篇关于IntentService:如何正确地排队?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆