我怎么能调用一个方法,每5秒在android系统? [英] How can I invoke a method every 5 seconds in android?

查看:125
本文介绍了我怎么能调用一个方法,每5秒在android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在必须发送GPS位置每5秒到服务器时,我选择(自动上发送按钮)的应用程序工作。我是新与Android,所以我不知道我怎么可以开/关按钮,我怎么能调用发送每隔5秒钟数据的方法时,按钮上。

它必须调用每5秒的方法:

 公共无效POSTDATA()抛出ClientProtocolException,IOException异常,异常{
    字符串的经度=UK;
    字符串纬度=UK;
    字符串altitiude =UK;
    字符串时间=;
    串速度=;    getCurrentLocation(); //获取当前位置和更新mobileLocation变量    如果(mobileLocation!= NULL){
        locManager.removeUpdates(locListener); //这需要停止获取位置数据和节省电池电量。         经度=+ mobileLocation.getLongitude();
         纬度=+ mobileLocation.getLatitude();
         altitiude =+ mobileLocation.getAltitude();
        串精度=精度+ mobileLocation.getAccuracy();
        时间=+ mobileLocation.getTime();
         速度=+(int)的(4 * mobileLocation.getSpeed​​());        editTextShowLocation.setText(经度+\\ n+纬度+\\ n
                + altitiude +\\ n+准确+\\ n+时间+\\ n+速度);
    }其他{
        editTextShowLocation.setText(对不起,位置没有确定);    }
        字符串URL = \"http://www.itrack.somee.com/post.aspx?id=\"+\"f1\"+\"&long=\"+longitude+\"&lat=\"+latitude+\"&alt=\"+altitiude+\"&speed=\"+speed;        //创建一个新的HttpClient和邮政头
        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpPost httppost =新HttpPost(URL);        尝试{        //执行HTTP POST请求
        HTT presponse响应= httpclient.execute(httppost);        }赶上(ClientProtocolException E){
        // TODO自动生成catch块
        }赶上(IOException异常五){
        // TODO自动生成catch块
        }}


解决方案

我曾经面临完全一样的问题,周期性地发送位置。我用的处理程序和方法postDelayed

我的code的定期调用部分看起来是这样的:

 私人最终FIVE_SECONDS = 5000;
公共无效scheduleSendLocation(){
    handler.postDelayed(新的Runnable(){
        公共无效的run(){
            sendLocation(); //此方法将包含您几乎完成HTTP调用
            handler.postDelayed(这一点,FIVE_SECONDS);
        }
    },FIVE_SECONDS);
}

然后你只需要调用scheduleSendLocation当你要开始你的时间的电话。

I'm working in an application which must send a GPS location every 5 seconds to the server when I choose (auto send button on). I'm new with android so I don't know how I can make an on/off button and how can I invoke the method that sends the data every 5 seconds when the button is on.

The method which it must invoke every 5 seconds:

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
        }



}

解决方案

I have faced exactly the same problem, sending location periodically. I've used a handler and its postDelayed method.

The periodic call part of my code looks like this:

private final 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);
}

Then you just need to call scheduleSendLocation when you want to start your period calls.

这篇关于我怎么能调用一个方法,每5秒在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆