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