我有一个异步任务,用于获取服务器位置并创建一个标记。这个异步任务在OnCreate()函数中只运行一次,并且可以正常工作。但是我需要每分钟运行一次该任务。我读到了需要使用计时器来实现,但我不知道如何实现。下面是异步任务的代码:
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
final String TAG = "AsyncTaskParseJson.java";
// set json string url
String yourJsonStringUrl = url;
JSONArray dataJsonArr = null;
@Override
protected void onPreExecute() {}
@Override
protected String doInBackground(String... arg0) {
try {
// instantiate json parser
LatLngActivity jParser = new LatLngActivity();
// get json string from url
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
// get the array of users
dataJsonArr = json.getJSONArray(ID_CAMION);
for (int i = 0; i < dataJsonArr.length(); i++) {
JSONObject c = dataJsonArr.getJSONObject(i);
// Storing JSON item in a Variable
String JSON_latitude = c.getString(TAG_LATITUDE);
String JSON_longitude = c.getString(TAG_LONGITUDE);
//converting JSON variables in double variables
double HTTP_latitude = Double.parseDouble(JSON_latitude);
double HTTP_longitude = Double.parseDouble(JSON_longitude);
HTTP_list.add(HTTP_latitude);
HTTP_list.add(HTTP_longitude);
// show the values in logcat
Log.e(TAG, "latitude: " + HTTP_list.get(0)
+ ", longitude: " + HTTP_list.get(1)
);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result){
createHTTPmarker();
}
}
onDestroy()
ж–№жі•дёӯпјҢдҪҝз”Ёhandler.removeCallbacks(previouslyStartedRunnable)
гҖӮе°ҶHandler
е’ҢRunnable
йғҪеЈ°жҳҺдёәзұ»еӯ—ж®өгҖӮиҝҷж ·е°ұеҸҜд»Ҙи§ЈеҶій—®йўҳдәҶ... :) - Yash Sampat