简体   繁体   English

android服务与背景中的重复线程与部分唤醒锁定

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

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. 我有一个运行在应用程序服务中的线程,该应用程序从之前使用webview登录的页面中读取数据。 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. 我知道我可能不得不用wake_lock来解决它,但我不知道如何。

I have 3 problems here. 我这里有3个问题。 I tried to repeat the thread with while(true)sleep(60000).... but that stops the thread after the screen of the phone goes off. 我尝试用while(true)sleep(60000)重复该线程....但是在手机屏幕关闭后停止线程。 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. 如果字符串计数大于零,则表示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. 我现在的问题是,在应用程序启动时,字符串“count”将返回null,因为用户尚未登录。 Therefore an exception will be thrown which stops the entire scheduled Task Executor. 因此会抛出一个异常,它会停止整个计划的Task Executor。 Is there a way to just restart it if "count" is null? 如果“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);

Don't use an explicit thread with a while + sleep to simulate a timer. 不要使用带有while + sleep的显式线程来模拟计时器。 It's ugly and unnecessary. 这是丑陋和不必要的。 There are more elegant ways that automatically schedule tasks every x time units, like ScheduledThreadPoolExecutor . 有更优雅的方法可以自动安排每x个时间单位的任务,比如ScheduledThreadPoolExecutor

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

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