上下文:如何创建某种Timer / AlarmManger机制,允许LocationClient在轮询位置之前连接两分钟。下面我已经粘贴了我在轮询位置之前延迟两分钟的尝试,但它在睡眠模式下似乎不起作用。
可能的解决方案:我可以使用AlarmManager。然而,这将迫使我创建一个SECOND服务,然后引用一个非常复杂的单个LocationClient。我也可以按照这个人的方法(Using Google Play Services LocationClient in background service),在onConnected方法中,他执行位置轮询。
我很想知道有关我应该研究什么的任何建议
在我的onStartCommand方法中......
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
//Getting and Posting Location
Log.i("localwordservice", "Creating and Connecting mLocationClient");
mLocationClient = new LocationClient(this, this, this);
mLocationClient.connect();
Timer theTimer = new Timer();
theTimer.schedule(new TimerTask() {
@Override
public void run() {
if(checkIfGooglePlay()) {
System.out.println("TIMER is now iniating post location");
getPostLocation();
System.out.println("wake lock being released!");
wl.release();
System.out.println("The Service is being stopped!");
stopSelf();
}
}
}, TWO_MINUTES);
答案 0 :(得分:2)
这不是LocationClient的工作原理。相反,您应该等待两分钟,连接到位置服务,然后通过调用getLastLocation()获取当前位置。或者,您可以立即调用requestLocationUpdates(),持续时间为2分钟,然后关闭连接或关闭更新。