1

一年多前的帖子表明,有必要使用 WakeLock 来保留 CPU 以收集位置信息(CommonsWare 特别是这里的Android Periodic GPS location updates with AlarmManager inside a Service)。但是我不明白为什么我不能简单地每 28 分钟调用一次服务,连接 LocationClient,设置计时器等待 2 分钟,然后获取最近的位置,断开客户端,然后停止服务。

因为 Commonsware 和在此处分叉他的示例的用户(https://github.com/alexbirkett/cwac-locpoll/blob/master/src/com/commonsware/cwac/locpoll/LocationPollerService.java)使用 LocationListener,而不是LocationClient,我想知道他的回答是否仍然适用。

谢谢你的时间!

编辑:就这么简单吗,AlarmManager 每 28 分钟调用一次服务 > 服务获取 WakeLock,并且在 LocationClient 连接时计时器经过两分钟 > 一旦两分钟过去,我可以终止服务并释放 WakeLock ?

它看起来像这样:

    PowerManager pm = (PowerManager) getBaseContext().getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();

    Timer theTimer = new Timer();
    theTimer.schedule(new TimerTask() {

        @Override
        public void run() {
            if(checkIfGooglePlay()) {
                getPostLocation();
                w1.release();
                stopSelf(); 
            }
        }
    }, TWO_MINUTES);

    return Service.START_NOT_STICKY;
4

1 回答 1

2

However I do not understand why I cannot simply call a Service every 28 minutes, connect the LocationClient, set a timer to wait 2 minutes, then get the most recent location, disconnect the client, and then stop the service.

You probably can poll the LocationClient. However, you will still need a WakeLock, as the device will fall asleep during those two minutes otherwise, you will still need AlarmManager for the "every 28 minutes" part, and I don't know if LocationClient will impose other requirements beyond those.

于 2013-06-28T17:07:54.620 回答