简体   繁体   English

android-如何通过时间限制过程

[英]android - how to limit a proccess by time

I'm trying to search for a user's location, my problem is that I want to limit the time search by time. 我正在尝试搜索用户的位置,我的问题是我想按时间限制搜索时间。 That is, if a good enough location was not found after ten seconds or so, return the best location found until that time. 也就是说,如果十秒钟左右后仍找不到足够好的位置,请返回找到的最佳位置。

I have an asynctask that activates a location listener. 我有一个asynctask激活一个位置监听器。 I'm trying to limit the time with android.os.Process.getElapsedCpuTime() but it only works when I get a location from my location listener that has arrived after the time set. 我正在尝试使用android.os.Process.getElapsedCpuTime()限制时间,但仅当我从位置侦听器中获取到在设置时间后到达的位置时,该方法才有效。

What I want to do is to keep running the time passed, regaredless of the location listener. 我想要做的是保持运行时间过去,而不管位置侦听器如何。 How can I do so? 我该怎么办?

Why cant u run a Timer (Java.util.Timer) where you can create a TimerTask. 为什么不能运行可以在其中创建TimerTask的Timer(Java.util.Timer)。 Start both AsyncTask and Timer at a time by scheduling the interval of 10 seconds. 通过计划10秒的时间间隔一次启动AsyncTask和Timer。 then after 10 seconds just check the status of the asynctask. 然后10秒钟后,只需检查asynctask的状态即可。 if it is still in RUNNING or PENDING state then cancel the asynctask. 如果它仍处于RUNNING或PENDING状态,则取消asynctask。 I think this will helpful to you. 我认为这将对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM