0

我创建了电话跟踪应用程序,我想从我跟踪的设备中获取来自互联网的数据(实际上是纬度和经度)。被跟踪的电话没问题,他们将每个 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{

    }

}

}
4

2 回答 2

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()删除回调!

于 2013-10-09T05:31:39.073 回答
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);
于 2013-10-09T05:35:51.510 回答