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获取纬度和经度
基础教程推荐
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
