Android使方法每X秒运行一次(涉及gps和服务器调用) [英] Android make method run every X seconds (involves gps and server call)

查看:462
本文介绍了Android使方法每X秒运行一次(涉及gps和服务器调用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个利基应用程序需要每隔几秒向服务器发送它的位置。 (不要担心手机的电池寿命)。我将如何每隔几秒就创建一个线程或执行某些操作?我有一个gpslistener,但它的子类的名称是onlocationchanged这似乎只想提供信息,如果位置发生变化,我将需要一个更新的位置发送到服务器每次虽然在我定义的时间间隔内



我该怎么做?



深入了解

解决方案放置在 onCreate 服务中。

  mTimer.scheduleAtFixedRate(new TimerTask(){
@Override
public void run(){
//想要去这里
}
},0,REFRESH_TIME);

REFRESH_TIME 是以毫秒为单位的频率或时间当运行重复自身。


I have a niche application that needs to send a server its location every few seconds. (Don't worry about a mobile phone's battery life). How would I make a thread or something execute every few seconds? I have a gpslistener but the name of its subclass is "onlocationchanged" which seems to only want to provide information if the location changed, I will need an updated location sent to the server every time though on an interval that I define

How would I do this?

insight appreciated

解决方案

Place this in onCreate of a service.

  mTimer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
           // What you want to do goes here
         }
    }, 0, REFRESH_TIME);

REFRESH_TIME is the frequency or time in milliseconds when the run repeats itself.

这篇关于Android使方法每X秒运行一次(涉及gps和服务器调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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