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 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01