每隔x分钟从互联网上获取数据

时间:2013-10-09 05:24:07

标签: java android

我创建了手机跟踪应用程序,我希望从我跟踪的设备中获取来自互联网的数据(实际上是纬度和经度)。跟踪的手机没问题,他们将每个onLocationChanged执行的纬度和经度发送到我的数据库服务器。 对于跟踪手机应用程序,我可以获得该数据,但只是在创建一次。我使用asynctask获取数据,问题是我每隔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);


}

这是我的asynctask

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{

    }

}

}

2 个答案:

答案 0 :(得分:0)

使用处理程序

   private Handler m_handler = new Handler();

// ..............

Runnable m_handlerTask = new Runnable() {

    @Override
    public void run() {
        m_handler.postDelayed(m_handlerTask, Xmins);
             new lokasibaru().execute();
    }
};


private void startGettingDataTask()
{
    m_handlerTask.run(); 
}

private void stopGettingDataTask()
{
    m_handler.removeCallbacks(m_handlerTask);
}

致电startGettingDataTask()开始,stopGettingDataTask()删除回调!

答案 1 :(得分:0)

查看Android Service,在服务中创建一个ScheduledExecutorService,它会启动请求页面并发送位置的Runnable任务。

ScheduledExecutorService,您可以像这样传递时间间隔,

//this will run the task every 5 minutes
scheduleTaskExecutor.scheduleAtFixedRate(task, 0, 5, TimeUnit.MINUTES);

以下API 9

//this will run the task every 5 minutes
scheduleTaskExecutor.scheduleAtFixedRate(task, 0, 300, TimeUnit.SECONDS);