LocationManager的服务 - 需要停止,如果在1分钟内未修复内未、LocationManager

2023-09-06 00:46:24 作者:够呛阿爸

我有定期的服务,我开始使用报警器每5分钟。 服务实现LocationListener的获得GPS定位,并将其保存到SQLite数据库。

I have regular service that I start using alarms every 5 minutes. Service Implements LocationListener to get GPS fix and save it into SqLite database.

我给服务1分钟,得到最好的解决成为可能。如果我得到的精度小于20之前 - 甚至更好

I give service 1 minute to get best fix possible. If I get accuracy <20 before that - even better.

所有这一切工作正常。我也有code,检查是否GPS禁用,因此我退出服务。

All that works fine. I also have code that checks if GPS disabled so I exit service.

我想要做的是:如果在1分钟内未修复 - 我要退出服务。目前,由于大多数的逻辑(校对时间等)是onLocationChanged - 我从来没有机会做,因为我从来没有得到位置

What I want to do is: If there is no fix within 1 minute - I want to exit service. Currently since most logic (time checks, etc) is in onLocationChanged - I never have chance to do so because I never get location.

有没有计时器之类的,我可以用的东西吗?我是新来的Java,例如将是很好。我认为是这样的:在开始创建计时器回调,并在这个回调函数我会清理和关闭。我不知道是否有将是可能的onLocationChanged运行产生任何影响?

Is there timer or something like that I can use? I'm new to Java, example would be nice. I see it like this: On start I create timer with callback and in this callback function I will cleanup and shutdown. I'm not sure if there going to be any implications with possible onLocationChanged running?

我还能当我得到onLocationChanged禁止定时器?

Can I possibly disable timer as soon as I get onLocationChanged?

推荐答案

使用 android.os.Handler :

Handler timeoutHandler = new Handler();
timeoutHandler.postDelayed(new Runnable() {
    public void run() {
        stopSelf();
    }
}, 60 * 1000);