1 继承Application.ActivityLifecycleCallbacks ,需要API > 14 , 现在基本上Android机都能达到这个要求。
public class ForegroundCallbacks implements Application.ActivityLifecycleCallbacks {
public static final long CHECK_DELAY = 500;
public static final String TAG = ForegroundCallbacks.class.getName();

public interface Listener {    public void onBecameForeground();    public void onBecameBackground();}private static ForegroundCallbacks instance;private boolean foreground = false, paused = true;private Handler handler = new Handler();private List listeners = new CopyOnWriteArrayList();private Runnable check;public static ForegroundCallbacks init(Application application) {    if (instance == null) {        instance = new ForegroundCallbacks();        application.registerActivityLifecycleCallbacks(instance);    }    return instance;}public static ForegroundCallbacks get(Application application) {    if (instance == null) {        init(application);    }    return instance;}public static ForegroundCallbacks get(Context ctx) {    if (instance == null) {        Context appCtx = ctx.getApplicationContext();        if (appCtx instanceof Application) {            init((Application) appCtx);        }        throw new IllegalStateException(                "Foreground is not initialised and " + "cannot obtain the Application object");    }    return instance;}public static ForegroundCallbacks get() {    if (instance == null) {        throw new IllegalStateException(                "Foreground is not initialised - invoke " + "at least once with parameterised init/get");    }    return instance;}public boolean isForeground() {    return foreground;}public boolean isBackground() {    return !foreground;}public void addListener(Listener listener) {    listeners.add(listener);}public void removeListener(Listener listener) {    listeners.remove(listener);}@Overridepublic void onActivityResumed(Activity activity) {    paused = false;    boolean wasBackground = !foreground;    foreground = true;    if (check != null)        handler.removeCallbacks(check);    if (wasBackground) {        A.e("went foreground");        for (Listener l : listeners) {            try {                l.onBecameForeground();            } catch (Exception exc) {                A.e("Listener threw exception!", exc);            }        }    } else {        A.e("still foreground");    }}@Overridepublic void onActivityPaused(Activity activity) {    paused = true;    if (check != null)        handler.removeCallbacks(check);    handler.postDelayed(check = new Runnable() {        @Override        public void run() {            if (foreground && paused) {                foreground = false;                A.e("went background");                for (Listener l : listeners) {                    try {                        l.onBecameBackground();                    } catch (Exception exc) {                        A.e("Listener threw exception!", exc);                    }                }            } else {                A.e("still foreground");            }        }    }, CHECK_DELAY);}@Overridepublic void onActivityCreated(Activity activity, Bundle savedInstanceState) {}@Overridepublic void onActivityStarted(Activity activity) {}@Overridepublic void onActivityStopped(Activity activity) {}@Overridepublic void onActivitySaveInstanceState(Activity activity, Bundle outState) {}@Overridepublic void onActivityDestroyed(Activity activity) {}

}

2 在Application中注册事件:
callback = ForegroundCallbacks.init(this);
callback.addListener(new Listener() {

        @Override        public void onBecameForeground() {            startService(new Intent(AppContext.this, CheckVipService.class));            System.out.println("---------------------------- 前台");        }        @Override        public void onBecameBackground() {            System.out.println("---------------------------- 后台");        }    });

更多相关文章

  1. Android(安卓)8.0 新特性
  2. Android内存管理机制
  3. Android进程管理机制及优化(HTC&其它可参考)
  4. Android(安卓)给贪吃蛇游戏加背景音乐
  5. android怎样将textview置于imageview之上
  6. ActionbarSherlock的Style讲解
  7. Android(安卓)TabWidget/TabHost的使用
  8. 后台动态添加布局文件、控件与动态设置属性
  9. android 保持屏幕唤醒状态

随机推荐

  1. SQL server 随机数函数
  2. SQL SERVER 文件和文件组
  3. mssql 两表合并sql语句
  4. sqlserver 比较两个表的列
  5. Sql Server 数据库索引整理语句,自动整理
  6. 查询数据排名情况SQL
  7. SQL中的left join right join
  8. SQL 合并多行记录的方法总汇
  9. 在sp_executesql中使用like字句的方法
  10. SQL Server 2008的逻辑查询处理步骤