Fad*_*lil 2 java android android-widget android-layout
我在一个应用程序中工作,当我选择(自动发送按钮打开)时,该应用程序必须每5秒向服务器发送一个GPS位置.我是android的新手,所以我不知道如何制作开/关按钮,如何在按钮打开时调用每5秒发送一次数据的方法.
它必须每5秒调用一次的方法:
public void postData() throws ClientProtocolException, IOException, Exception {
String longitude="UK";
String latitude="UK";
String altitiude="UK";
String time="";
String speed="";
getCurrentLocation(); // gets the current location and update mobileLocation variables
if (mobileLocation != null) {
locManager.removeUpdates(locListener); // This needs to stop getting the location data and save the battery power.
longitude = ""+mobileLocation.getLongitude();
latitude = "" + mobileLocation.getLatitude();
altitiude = "" + mobileLocation.getAltitude();
String accuracy = "Accuracy" + mobileLocation.getAccuracy();
time = "" + mobileLocation.getTime();
speed =""+ (int)(4*mobileLocation.getSpeed());
editTextShowLocation.setText(longitude + "\n" + latitude + "\n"
+ altitiude + "\n" + accuracy + "\n" + time+ "\n" + speed);
} else {
editTextShowLocation.setText("Sorry, location is not determined");
}
String url = "http://www.itrack.somee.com/post.aspx?id="+"f1"+"&long="+longitude+"&lat="+latitude+"&alt="+altitiude+"&speed="+speed;
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Run Code Online (Sandbox Code Playgroud)
Edu*_*rdo 14
我遇到了完全相同的问题,定期发送位置.我使用过处理程序及其postDelayed方法.
我的代码的定期调用部分如下所示:
private final int FIVE_SECONDS = 5000;
public void scheduleSendLocation() {
handler.postDelayed(new Runnable() {
public void run() {
sendLocation(); // this method will contain your almost-finished HTTP calls
handler.postDelayed(this, FIVE_SECONDS);
}
}, FIVE_SECONDS);
}
Run Code Online (Sandbox Code Playgroud)
然后,scheduleSendLocation当您想要开始期间通话时,您只需要打电话.
| 归档时间: |
|
| 查看次数: |
21173 次 |
| 最近记录: |