【发布时间】:2013-07-02 00:18:24
【问题描述】:
我试图在调用 getLastLocation 之前给 LocationClient 两分钟的时间进行连接。最初我使用 Timer(和 TimerTask)实现了这一点,但由于 Timer 在睡眠模式下无法工作,我想将其转换为 AlarmManager。但是,考虑到 AlarmManager 调用另一个类,我对如何执行此操作有点困惑,而我想留在同一个类中并简单地延迟两分钟。
这是使用计时器的外观。
Timer theTimer = new Timer();
theTimer.schedule(new TimerTask() {
@Override
public void run() {
if(checkIfGooglePlay() && checkTime()) {
getPostLocation();
stopSelf();
mLocationClient.disconnect();
}
}
}, TWO_MINUTES);
【问题讨论】:
-
AlarmManager 可以使用pendingIntent调用同一个类
-
所以我可以调用 mLocationClient.connect(),然后在两分钟后在该类上创建一个带有 AlarmManager 设置的 PendingIntent?
-
我不知道你的正确要求,但到我理解的程度,你应该使用一个服务类来完成这个。可以使用 AlarmManager 类或以正常方式唤醒,使用接口从您的 Activity 与 Service 进行通信...
标签: android timer location alarmmanager google-play-services