我是靠谱客的博主 可靠月饼,最近开发中收集的这篇文章主要介绍Android LifecycleObserver & ScheduledThreadPoolExecutor用户可见时每隔3秒周期更新,用户不可见时暂停周期更新Android LifecycleObserver & ScheduledThreadPoolExecutor用户可见时每隔3秒周期更新,不可见时暂停每隔3秒的周期更新,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Android LifecycleObserver & ScheduledThreadPoolExecutor用户可见时每隔3秒周期更新,不可见时暂停每隔3秒的周期更新

简言之,当用户按home键把当前App切换至“后台”不可见时候(用户看不见当前App页面时候),暂停每隔3秒的周期更新操作。当用户又将App切换至前台,用户看到App页面时候,重启每隔3秒的周期性更新。关键是通过LifecycleObserver使用ScheduledThreadPoolExecutor的scheduleAtFixedRate实现。

package zhangphil.test;

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.LifecycleRegistry;
import android.arch.lifecycle.OnLifecycleEvent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class LiveActivity extends AppCompatActivity {
    private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("onCreate");

        MyLifecycleObserver mLifecycleObserver = new MyLifecycleObserver();
        getLifecycle().addObserver(mLifecycleObserver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        System.out.println("onPause");
    }

    @Override
    public LifecycleRegistry getLifecycle() {
        return mLifecycleRegistry;
    }

    public class MyLifecycleObserver implements LifecycleObserver {
        private UpdateThread mUpdateThread;

        public MyLifecycleObserver() {
            mUpdateThread = new UpdateThread();
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
        public void on_create() {
            System.out.println("Lifecycle.Event.ON_CREATE");
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
        public void on_resume() {
            System.out.println("Lifecycle.Event.ON_RESUME");
            mUpdateThread.startUpdate();
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
        public void on_pause() {
            System.out.println("Lifecycle.Event.ON_PAUSE");
            mUpdateThread.stopUpdate();
        }
    }

    private class UpdateThread extends Thread {
        private ScheduledThreadPoolExecutor mScheduledThreadPoolExecutor = null;
        private Runnable mRunnable = null;
        private ScheduledFuture<?> mScheduledFuture = null;

        public UpdateThread() {
            mScheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);
            mRunnable = new Runnable() {
                @Override
                public void run() {
                    updateAction();
                }
            };
        }

        public void startUpdate() {
            //每隔3秒周期性的执行更新动作。
            mScheduledFuture = mScheduledThreadPoolExecutor.scheduleAtFixedRate(mRunnable, 0, 3, TimeUnit.SECONDS);
        }

        public void stopUpdate() {
            mScheduledFuture.cancel(true);
        }
    }

    /**
     * 从远程服务器开始更新数据到本地的动作。
     */
    private void updateAction() {
        System.out.println(Thread.currentThread().getName() + " 从远程服务器更新数据...");
    }
}

 

输出:

2018-11-12 18:59:17.041 16218-16218/? I/System.out: onCreate
2018-11-12 18:59:17.047 16218-16218/? I/System.out: Lifecycle.Event.ON_CREATE
2018-11-12 18:59:17.188 16218-16218/? I/System.out: onResume
2018-11-12 18:59:17.188 16218-16218/? I/System.out: Lifecycle.Event.ON_RESUME
2018-11-12 18:59:17.191 16218-16241/? I/System.out: pool-1-thread-1 从远程服务器更新数据...
2018-11-12 18:59:20.189 16218-16241/zhangphil.test I/System.out: pool-1-thread-1 从远程服务器更新数据...
2018-11-12 18:59:23.191 16218-16241/zhangphil.test I/System.out: pool-1-thread-1 从远程服务器更新数据...
2018-11-12 18:59:24.970 16218-16218/zhangphil.test I/System.out: Lifecycle.Event.ON_PAUSE
2018-11-12 18:59:24.970 16218-16218/zhangphil.test I/System.out: onPause
2018-11-12 18:59:47.126 16218-16218/zhangphil.test I/System.out: onResume
2018-11-12 18:59:47.127 16218-16218/zhangphil.test I/System.out: Lifecycle.Event.ON_RESUME
2018-11-12 18:59:47.128 16218-16241/zhangphil.test I/System.out: pool-1-thread-1 从远程服务器更新数据...
2018-11-12 18:59:50.128 16218-16241/zhangphil.test I/System.out: pool-1-thread-1 从远程服务器更新数据...
2018-11-12 18:59:53.128 16218-16241/zhangphil.test I/System.out: pool-1-thread-1 从远程服务器更新数据...
2018-11-12 18:59:55.211 16218-16218/zhangphil.test I/System.out: Lifecycle.Event.ON_PAUSE
2018-11-12 18:59:55.211 16218-16218/zhangphil.test I/System.out: onPause

 

 

最后

以上就是可靠月饼为你收集整理的Android LifecycleObserver & ScheduledThreadPoolExecutor用户可见时每隔3秒周期更新,用户不可见时暂停周期更新Android LifecycleObserver & ScheduledThreadPoolExecutor用户可见时每隔3秒周期更新,不可见时暂停每隔3秒的周期更新的全部内容,希望文章能够帮你解决Android LifecycleObserver & ScheduledThreadPoolExecutor用户可见时每隔3秒周期更新,用户不可见时暂停周期更新Android LifecycleObserver & ScheduledThreadPoolExecutor用户可见时每隔3秒周期更新,不可见时暂停每隔3秒的周期更新所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部