簡體   English   中英

android服務與背景中的重復線程與部分喚醒鎖定

[英]android service with repeating thread in background with partial wake lock

我有一個運行在應用程序服務中的線程,該應用程序從之前使用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);

不要使用帶有while + sleep的顯式線程來模擬計時器。 這是丑陋和不必要的。 有更優雅的方法可以自動安排每x個時間單位的任務,比如ScheduledThreadPoolExecutor

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM