我必須定期向服務器發送我目前的位置詳細信息(例如,經緯度:&)(例如:每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. 使用Android將移動當前位置發送到Web服務器
- 3. 如何發送位置到服務器?
- 4. Android位置服務,如何發送位置到MySQL位置
- 5. 發送位置更新到服務器在Android中
- 6. 在XMPP中定期發送當前位置
- 7. 如何發送Android應用程序的位置到服務器?
- 8. Android:如何使用csrf驗證週期性發送位置到django服務器
- 9. 存儲當前的ArrayList之前發送到服務器
- 10. Javascript - 定期發送用戶位置數據到服務器(多線程)
- 11. 將GPS位置發送到Web服務
- 12. 後臺服務需要在服務器上發送GPS位置
- 13. 當iphone位於後臺時用CLLocationManager發送位置到服務器
- 14. 發送javascript日期到asp.net服務器
- 15. 獲取當前位置並將其發佈到網絡服務
- 16. 從後臺服務中找到當前位置android
- 17. 該應用程序發送位置後,服務器Android Java
- 18. 如何發送當前網址到服務器?
- 19. 通過短信發送我當前所在的位置 - Android
- 20. 發送XML到PHP服務器與Android
- 21. Android - 發送JSON請求到服務器
- 22. Android將圖像發送到服務器
- 23. 發送文件到服務器的android
- 24. Android:發送數據到服務器
- 25. 發送json到服務器(android)
- 26. 發送JSON到服務器通過HTTP放置請求在android
- 27. GPS中間件服務器 - 保存位置併發送數據到服務器
- 28. 如何在android中獲取當前位置在後臺服務
- 29. 通過短信發送當前位置
- 30. 通過短信發送當前位置
請檢查了這一點,很多類似的應用程序和工作正常,但我需要相關的一些功能,你會讀我的文章後,知道一些幫助,並把這個事情回答,因爲我可以對不起,找不到了選項的評論。 。 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