我创建了电话跟踪应用程序,我想从我跟踪的设备中获取来自互联网的数据(实际上是纬度和经度)。被跟踪的电话没问题,他们将每个 onLocationChanged 执行的纬度和经度发送到我的数据库服务器。对于跟踪电话应用程序,我可以获取该数据,但仅在 oncreate 一次时。我使用异步任务获取数据,问题是我很难每 X 分钟获取一次数据,我应该使用处理程序吗?有人可以帮我写一些代码吗?
到目前为止,这是我的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String Url = "http://mysite.com/tes.php";
mf =(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
gmap = mf.getMap();
gmap.setMyLocationEnabled(true);
gmap.setMapType(gmap.MAP_TYPE_SATELLITE);
lat= (TextView) findViewById(R.id.lati);
longit = (TextView) findViewById(R.id.longi);
// TODO Auto-generated method stub
lokasibaru lokasi = new lokasibaru();
lokasi.execute(Url);
}
这是我的异步任务
private class lokasibaru extends AsyncTask <String,ArrayList<String>,ArrayList<String>>{
String latitude,longitude;
protected void onProgressUpdate(String... values) {
}
protected void onPostExecute(ArrayList<String> hasil) {
latitude = hasil.get(0);
longitude = hasil.get(1);
lat.setText(latitude);
longit.setText(longitude);
}
@Override
protected ArrayList<String> doInBackground(String... value) {
ArrayList<String> result = null ;
String Url = value[0];
koneksi get = new koneksi();
try {
//result = GetR.getdata(Url);
result = get.getdata(Url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
这是我从互联网上获取数据的 koneksi 课程
public class koneksi {
public ArrayList<String> getdata(String url) throws Exception{
BufferedReader in = null;
String data = null;
ArrayList<String> kordinat = new ArrayList<String> ();
int count ;
try{
HttpClient client= new DefaultHttpClient();
URI site = new URI(url);
HttpGet req = new HttpGet();
req.setURI(site);
HttpResponse resp = client.execute(req);
count =0;
in = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null ){
//sb.append(l+nl);
kordinat.add(l);
count++;
}
in.close();
data = sb.toString();
return kordinat;
}
finally{
}
}
}