我必须定期向服务器发送我目前的位置详细信息(例如,经纬度:&)(例如:每5分钟)。有没有最好的方法?我知道如何获取当前位置&如何将详细信息发送到服务器。但是,我如何周期性地重复此操作?定期在android发送当前位置到服务器
回答
当用户第一次打开应用程序时,使用AlarmManager
在5分钟后唤醒警报。创建服务(获取位置并更新到服务器)以在警报通知您的应用程序时运行。服务完成后,再次注册闹钟,5分钟后唤醒。通过这种方式你可以实现你的任务。
REF
Android: How to periodically send location to a server
http://developer.android.com/reference/android/app/AlarmManager.html
http://developer.android.com/reference/android/app/Service.html
第一编辑 - 添加代码示例
步骤1 - 创建警报管理器和寄存器报警
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
/* Setting alarm for every one hour from the current time.*/
int intervalTimeMillis = 1000 * 60 * 60; // 1 hour
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calander.getTimeInMillis(), intervalTimeMillis,
alarmIntent);
}
2步 - 创建Receiver类
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 - 创建服务类
public class YourService 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 - 注册服务和接收机
<service android:name="com.envision.ghari.services.YourService"></service>
<receiver android:name="com.envision.ghari.receivers.YourWakefulReceiver"></receiver>
注意:该代码是了解执行情况。它不会编译。
我希望这答案有点充实。 – 2012-06-06 21:22:43
也会添加网络可用性检查。如果网络不可用,则在本地存储坐标并在网络存在时将它们发送到服务器 – Harsh 2016-11-11 07:35:00
可能是开始的最佳方式。 – 2017-01-14 20:23:09
因为它不需要UI交互,你应该为此创建一个服务,服务将调用requestLocationUpdates方法,在这个方法中,传递参数的最短时间为5分钟。
使用BroadcastReceiver 是发送定期请求的好选择。
Here是一个使用BroadcastReceiver的教程。
使用AlarmManager
唤醒服务的最佳方法,并将获取的位置发布到服务器。
我正在开发一个应用程序,该应用程序可以在10秒和15秒内在服务中获取手机的位置,它工作正常,但有时网络提供程序侦听器的方法OnLocationChanged停止被调用。
import android.location.Address;
import android.location.Geocoder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import java.io.IOException;
import java.util.List;
import foodinn.databases.LocationUpdaterDatabase;
public class LocationUpdatesActivity extends AppCompatActivity {
private LocationUpdaterDatabase locationUpdaterDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_updates);
locationUpdaterDatabase = new LocationUpdaterDatabase(this, "Locations.db", null, 1);
final Geocoder geocoder = new Geocoder(this);
ListView listView = (ListView) findViewById(R.id.locationUpdatesListView);
listView.setAdapter(new SimpleCursorAdapter(this, R.layout.location_updates_list_view_view, locationUpdaterDatabase.getLocationUpdates(), new String[] {"latitude", "longitude", "whenUpdated"}, new int[] {R.id.listViewTextView1, R.id.listViewTextView2, R.id.listViewTextView3}, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
double latitude = Double.valueOf(((TextView) view.findViewById(R.id.listViewTextView1)).getText().toString());
double longitude = Double.valueOf(((TextView) view.findViewById(R.id.listViewTextView2)).getText().toString());
try {
List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1);
String address = addressList.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addressList.get(0).getLocality();
String state = addressList.get(0).getAdminArea();
String country = addressList.get(0).getCountryName();
String postalCode = addressList.get(0).getPostalCode();
String knownName = addressList.get(0).getFeatureName();
((TextView) view.findViewById(R.id.listViewTextView4)).setText(address + ", " + city + ", " + country);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
尽管此代码片段可以解决该问题,[包括解释](http://meta.stackexchange .com/questions/114762/explain-completely-code-based-answers)确实有助于提高帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。 – 2017-01-19 15:12:15
- 1. Android:如何定期向服务器发送位置信息
- 2. 如何发送位置到服务器?
- 3. Android位置服务,如何发送位置到MySQL位置
- 4. 发送位置更新到服务器在Android中
- 5. 在XMPP中定期发送当前位置
- 6. 如何发送Android应用程序的位置到服务器?
- 7. Android:如何使用csrf验证周期性发送位置到django服务器
- 8. 存储当前的ArrayList之前发送到服务器
- 9. 将GPS位置发送到Web服务
- 10. 当iphone位于后台时用CLLocationManager发送位置到服务器
- 11. 获取当前位置并将其发布到网络服务
- 12. 该应用程序发送位置后,服务器Android Java
- 13. 如何发送当前网址到服务器?
- 14. Android:发送数据到服务器
- 15. Android - 发送JSON请求到服务器
- 16. 发送文件到服务器的android
- 17. Android将图像发送到服务器
- 18. 通过短信发送当前位置
- 19. GPS中间件服务器 - 保存位置并发送数据到服务器
- 20. 获得当前位置定位的Android
- 21. 在特定时间发送iPhone当前位置
- 22. 通过PHP CURL发送地理位置到服务器
- 23. 如何确保发送到服务器的GPS位置准确?
- 24. 连续发送位置到服务器..好还是坏?
- 25. 尝试发送笔记本电脑的当前位置到服务器并显示在谷歌地图
- 26. 最常推荐的方法是在Android中连续向服务器发送定期位置信息?
- 27. 发送img到服务器
- 28. 发送UIImage到服务器
- 29. 发送xml到服务器
- 30. 发送LatLong到服务器
请检查了这一点,很多类似的应用程序和工作正常,但我需要相关的一些功能,你会读我的文章后,知道一些帮助,并把这个事情回答,因为我可以对不起,找不到了选项的评论。 。 http://stackoverflow.com/questions/14088095/how-can-i-send-the-gps-and-network-location-cordinates-to-a-server-static-ip/14103445#14103445 – user2010282 2013-01-04 12:02:05