在背景android中每10分钟使用gps获取经度和纬度 [英] getting latitude and longitude using gps every 10 minute in background android

查看:133
本文介绍了在背景android中每10分钟使用gps获取经度和纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



要求: / strong>



每隔10分钟获取经度和经度,如果应用程序关闭或不在。



服务,线程,AlarmManager等。



我的应用程序运行良好,持续1-2小时,然后自动获取经度和纬度,但我的服务仍在运行。



如果有人知道,那么请给我指导线如何在背景中每隔10分钟获取一次纬度和经度。

LocationActivity.java



当用户点击开始按钮时。

  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



$ p
$ b

  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



取得经度和纬度。

解决方案

我正在使用这种方式解决我的问题。 / p>

  PendingIntent pi = PendingIntent.getBroadcast(context,0,i,0); 
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),PERIOD,pi);

我不知道这是对还是错,但为我工作。
感谢@ Lucifer,@ Maya mAku和@piotrpo作为您的指导。


I am done getting latitude and longitude using LocationManager and working proper.

Requirement:

get latitude and longitude every 10 minute if application is close or not.

I am try services,Thread,AlarmManager etc.

My application is work good for 1-2 hours then after automatically not get latitude and longitude but my services is still running.

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

i am start my services.

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

getting latitude and longitude.

解决方案

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);

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获取经度和纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆