【问题标题】:How can I invoke a method every 5 seconds in android?如何在android中每5秒调用一次方法?
【发布时间】:2012-11-16 19:06:04
【问题描述】:

我正在使用一个应用程序,当我选择(自动发送按钮打开)时,它必须每 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
        }



}

【问题讨论】:

    标签: java android android-layout android-widget


    【解决方案1】:

    我遇到了完全相同的问题,定期发送位置。我使用了一个处理程序及其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

    【讨论】:

    • 谢谢。但是我怎么能在我调用它之后停止这个方法!?
    • @FadiKhalil handler.removeCallbacksAndMessages(null) 应该这样做。
    • 会每 5 秒调用一次 sendLocation 吗?因为我认为每次 ti 都会延迟“执行 sendLocation() 所需的时间”函数。
    • @Parth,是的,它将每 5 秒调用一次。
    【解决方案2】:

    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 秒才能运行,因此您可能只想在该时间间隔内运行它。另外仅供参考,这会耗尽电池

    【讨论】:

      【解决方案3】:

      查看AlarmManager 类http://developer.android.com/reference/android/app/AlarmManager.html,尤其是setRepeating 函数。它会比你想的要复杂一些。

      【讨论】:

        【解决方案4】:

        您可以使用Timer。但我认为如果你只发送它的位置与你发送的最后一个位置改变了一定距离会更好。这样,您可以发送更少的数据而不会丢失任何信息。

        【讨论】:

          猜你喜欢
          • 2019-02-01
          • 2013-04-25
          • 2019-04-21
          • 2020-03-12
          • 1970-01-01
          • 2011-08-06
          • 1970-01-01
          • 2018-05-17
          相关资源
          最近更新 更多