簡體   English   中英

如何在Android中每5秒調用一次方法?

[英]How can I invoke a method every 5 seconds in android?

我正在使用一個應用程序,當我選擇(自動發送按鈕打開)時,該應用程序必須每5秒向服務器發送一次GPS位置。 我是android的新手,所以我不知道如何制作開/關按鈕,以及如何在打開按鈕時調用每5秒發送一次數據的方法。

它必須每5秒調用一次的方法:

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
        }



}

我遇到了完全相同的問題,需要定期發送位置信息。 我使用了處理程序及其postDelayed方法。

我的代碼的定期調用部分如下所示:

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

然后,當您要開始scheduleSendLocation呼叫時,只需要調用scheduleSendLocation即可。

Ridcully是正確的,可能沒有理由每5秒發送一次當前位置。 這是其背后的原因:

您實際上只關心用戶位置的兩件事:

  1. 他們現在在哪里?

  2. 自從我到達第一個位置以來,他們有沒有搬家?

因此,一旦您獲得滿意的初始位置,就可以在用戶移動時進行注冊以獲取回調,如下所示:

private LocationListener mLocationListener;

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(final Location location) {
            updateLocation(location);
        }

        @Override
        public void onStatusChanged(final String provider,
                final int status, final Bundle extras) {
        }

        @Override
        public void onProviderEnabled(final String provider) {
        }

        @Override
        public void onProviderDisabled(final String provider) {
        }
    };
}

話雖如此,您顯然可以執行其他人所說的,並每5秒運行一次計時器。 問題是,大多數好的位置都需要10到20秒才能運行,因此您可能只想在此間隔內運行它。 僅供參考,這會殺死電池

查看AlarmManager類http://developer.android.com/reference/android/app/AlarmManager.html ,尤其是setRepeating函數。 它會比您想要的復雜一些。

您可以使用Timer 但是我認為,如果只發送其位置與上次發送的位置相距一定距離,會更好。 這樣,您可以發送更少的數據而不會丟失任何信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM