问题描述
我已经完成使用 LocationManager
获取纬度和经度并正常工作.
I am done getting latitude and longitude using LocationManager
and working proper.
要求:
如果应用程序关闭与否,每 10 分钟获取一次纬度和经度.
get latitude and longitude every 10 minute if application is close or not.
我正在尝试服务、线程、AlarmManager 等.
I am try services,Thread,AlarmManager etc.
我的应用程序可以正常工作 1-2 小时,然后自动获取不到纬度和经度,但我的服务仍在运行.
My application is work good for 1-2 hours then after automatically not get latitude and longitude but my services is still running.
如果有人知道,请给我指导如何在后台每 10 分钟获取一次纬度和经度.
If any one know then please give me guide line how to fetch latitude and longitude every 10 minute in background.
LocationActivity.java
当用户点击开始按钮时.
when user click on start button.
Intent i=new Intent(context, OnAlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()+60000,PERIOD,pi);
OnAlarmReceiver.java
我正在开始我的服务.
public class OnAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, MyService.class));
}
}
MyService.java
@Override
public void onStart(Intent intent, int startId) {
// super.onStart(intent, startId);
new MyTime(mContext);
}
MyTime.java
获取纬度和经度.
推荐答案
我用这种方式解决了我的问题.
I am using this way and solve my problem.
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), PERIOD , pi);
我不知道这是对还是错.但为我工作.感谢 @Lucifer、@Maya mAku 和 @piotrpo 的指导.
I am don't know this is right or wrong.but work for me. Thanks @Lucifer,@Maya mAku and @piotrpo for your guidline.
这篇关于在后台android中每10分钟使用gps获取纬度和经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!