1

我知道这个问题已被问过很多次,但我找不到适合我的问题的答案。

我有一个应用程序,它必须每 30 分钟知道一次用户的位置(为了做特定的事情)。我不希望应用程序检查位置是否已更改。我只需要每 30 分钟获取一次,所以我不想使用 onLocationChanged() 方法

我使用“被动”方式而不是 gps 或网络方式,所以我不需要“找到”最好的提供商,我已经知道了。

有办法吗?

非常感谢。

朱利安

4

1 回答 1

2

You could set up an alarm for every 30 minutes and check getLastKnownLocation. However, the last known location is only actively updated if at least 1 app is registered for location updates- so you'll still have to register yourself to make sure that variable is updated.

AlarmManager mgr=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
registerReceiver( receiver, new IntentFilter("broadcast reciever class name here") );
Intent i=new Intent("broadcast reciever class name here");
PendingIntent pi=PendingIntent.getBroadcast(this, 0, i, 0);
mgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30*60*1000, pi);

This will set an alarm for every 30 minutes. If you want this to continue to go off after the activity is dead, set it in a Service.

于 2013-05-23T19:03:35.700 回答