Send Current Location to server periodically in android(在android中定期将当前位置发送到服务器)
问题描述
我必须定期(例如:每 5 分钟)将我当前的位置详细信息(纬度和经度)发送到服务器.有没有最好的方法?我知道如何获取当前位置和如何将详细信息发送到服务器.但是如何定期重复此操作?
I have to send my current location details (lat & long) to server periodically (Ex: for every 5 minutes). Is there any best way? I know how to get the current location & how to send the details to server. But how do I repeat this in periodic intervals?
推荐答案
使用AlarmManager
注册一个闹钟,当用户第一次打开应用程序5分钟后唤醒.创建一个服务(获取位置并更新到服务器)以在警报通知您的应用程序时运行.服务完成后,再次注册闹钟,5分钟后唤醒.通过这种方式,您可以完成您的任务.
Register an alarm using AlarmManager
to wake up after 5min when user open the application first time. create a service(fetch location and update to server) to run when alarm notifies your application. After the service finished the work , register for an alarm again to wake up after 5min. by this way you can achieve your task.
参考
Android:如何定期将位置发送到服务器
http://developer.android.com/reference/android/app/AlarmManager.html
http://developer.android.com/reference/android/app/Service.html
第一次编辑 - 添加代码示例
第 1 步 - 创建警报管理器并注册警报
Step 1 - Create alarm manager and register alarm
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(Main.this, YourWakefulReceiver.class);
bool flag = (PendingIntent.getBroadcast(Main.this, 0,
intent, PendingIntent.FLAG_NO_CREATE)==null);
/*Register alarm if not registered already*/
if(flag){
PendingIntent alarmIntent = PendingIntent.getBroadcast(Main.this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create Calendar obj called calendar
Calendar calendar = Calendar.getInstance();
/* Setting alarm for every one hour from the current time.*/
int intervalTimeMillis = 1000 * 60 * 60; // 1 hour
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), intervalTimeMillis,
alarmIntent);
}
第 2 步 - 创建接收器类
Step 2 - Create Receiver class
public class YourWakefulReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, SimpleWakefulService.class);
startWakefulService(context, service);
}
}
}
Setp 3 - 创建服务类
Setp 3 - Create Service class
public class SimpleWakefulService extends IntentService {
private static String tagName = "YourService";
public SimpleWakefulService() {
super("YourService");
}
@Override
protected void onHandleIntent(Intent intent) {
// Start your location
LocationUtil.startLocationListener();
try {
// Wait for 10 seconds
Thread.sleep(1000*10);
} catch (InterruptedException e) {
}
//Stop location listener
LocationUtil.stopLocationListener();
// upload or save location
uploadGps();
SimpleWakefulReceiver.completeWakefulIntent(intent);
}
}
第 4 步 - 注册服务和接收器
Step 4 - Register service and receiver
<service android:name="com.envision.ghari.services.SimpleWakefulService"></service>
<receiver android:name="com.envision.ghari.receivers.YourWakefulReceiver"></receiver>
注意:这段代码是为了理解实现.它不会编译.
Note : This code is to understand the implementation. It will not compile.
这篇关于在android中定期将当前位置发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!