我有一个在应用程序服务中运行的线程,该应用程序从之前使用webview登录的页面中读取数据。 该线程工作正常。
现在我想定期重复这个帖子,说一分钟,即使手机处于睡眠/屏幕关闭状态。 我知道我可能不得不用wake_lock来解决它,但我不知道如何。
我这里有3个问题。 我尝试用while(true)sleep(60000)重复该线程....但是在手机屏幕关闭后停止线程。 有没有更好的办法?
然后我还想将字符串计数与零进行比较。 意味着如果字符串计数大于零,则执行xxx。
任何帮助非常感谢!
Thread downloadThread = new Thread() { public void run() { Document doc; doc = null; try { final String url = "https://xxx.xxx.xx"; // -- Android Cookie part here -- CookieSyncManager.getInstance().sync(); CookieManager cm = CookieManager.getInstance(); String cookie = cm.getCookie(url); // Jsoup uses cookies as "name/value pairs" doc = Jsoup.connect("https://xxx.xxx.xx").cookie(url, cookie).get(); Elements elements = doc.select("span.tabCount"); String count = elements.first().text(); Log.d(TAG, "wart"+(count)); Log.d(TAG, "wartcookiedate:"+(cookie)); } catch (IOException e) { e.printStackTrace(); } } }; downloadThread.start();这是我第二次尝试下面的代码。 当用户已经登录时,它可以完美地工作。 我现在的问题是,在应用程序启动时,字符串“count”将返回null,因为用户尚未登录。 因此,将抛出一个异常,它将停止整个计划的Task Executor。 如果“count”为空,有没有办法重新启动它?
scheduleTaskExecutor= Executors.newScheduledThreadPool(5); // This schedule a task to run every 10 seconds: scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() { public void run() { Document doc; doc = null; try { final String url = "https://xxx.xxx.xx"; // -- Android Cookie part here -- CookieSyncManager.getInstance().sync(); CookieManager cm = CookieManager.getInstance(); String cookie = cm.getCookie(url); // returns cookie for url // Jsoup uses cookies as "name/value pairs" doc = Jsoup.connect("https://xxx.xxx.xx").cookie(url, cookie).get(); Elements elements = doc.select("span.tabCount"); String count = elements.first().text(); Log.d(TAG, "wart"+(count)); Log.d(TAG, "wartcookiedate:"+(cookie)); } catch (IOException e) { e.printStackTrace(); } } }, 0, 10, TimeUnit.SECONDS);I have a thread running in a service of an app that reads out data from a page that has been logged into with a webview before. That thread is working fine.
Now i would like to repeat this thread periodically, say once minute, even while the phone is asleep/screen off. I know i would probably have to go about it with wake_lock but i have no clue how.
I have 3 problems here. I tried to repeat the thread with while(true)sleep(60000).... but that stops the thread after the screen of the phone goes off. Is there a better way?
Then i would also like to compare the string count to zero. Meaning if string count is greater than zero do xxx.
Any help is very appreciated!
Thread downloadThread = new Thread() { public void run() { Document doc; doc = null; try { final String url = "https://xxx.xxx.xx"; // -- Android Cookie part here -- CookieSyncManager.getInstance().sync(); CookieManager cm = CookieManager.getInstance(); String cookie = cm.getCookie(url); // Jsoup uses cookies as "name/value pairs" doc = Jsoup.connect("https://xxx.xxx.xx").cookie(url, cookie).get(); Elements elements = doc.select("span.tabCount"); String count = elements.first().text(); Log.d(TAG, "wart"+(count)); Log.d(TAG, "wartcookiedate:"+(cookie)); } catch (IOException e) { e.printStackTrace(); } } }; downloadThread.start();Here is my second try with the code below. When the user is already logged in it workds perfectly. My problem now is that on start of the app the string "count" will be returned null since the user is not logged in yet. Therefore an exception will be thrown which stops the entire scheduled Task Executor. Is there a way to just restart it if "count" is null?
scheduleTaskExecutor= Executors.newScheduledThreadPool(5); // This schedule a task to run every 10 seconds: scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() { public void run() { Document doc; doc = null; try { final String url = "https://xxx.xxx.xx"; // -- Android Cookie part here -- CookieSyncManager.getInstance().sync(); CookieManager cm = CookieManager.getInstance(); String cookie = cm.getCookie(url); // returns cookie for url // Jsoup uses cookies as "name/value pairs" doc = Jsoup.connect("https://xxx.xxx.xx").cookie(url, cookie).get(); Elements elements = doc.select("span.tabCount"); String count = elements.first().text(); Log.d(TAG, "wart"+(count)); Log.d(TAG, "wartcookiedate:"+(cookie)); } catch (IOException e) { e.printStackTrace(); } } }, 0, 10, TimeUnit.SECONDS);最满意答案
不要使用带有while + sleep的显式线程来模拟计时器。 这是丑陋和不必要的。 有更优雅的方法可以自动安排每x个时间单位的任务,比如ScheduledThreadPoolExecutor 。
Don't use an explicit thread with a while + sleep to simulate a timer. It's ugly and unnecessary. There are more elegant ways that automatically schedule tasks every x time units, like ScheduledThreadPoolExecutor.
更多推荐
发布评论