getting latitude and longitude using gps every 10 minute in background android(在后台android中每10分钟使用gps获取纬度和经度)
问题描述
我已经完成使用 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获取纬度和经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在后台android中每10分钟使用gps获取纬度和经度


- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01