4

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);
4

1 回答 1

4

不要使用带有while+的显式线程sleep来模拟计时器。这是丑陋和不必要的。还有更优雅的方法可以每隔 x 个时间单位自动安排任务,例如ScheduledThreadPoolExecutor

于 2012-09-20T17:32:06.917 回答