App前后台判断

http://blog.csdn.net/vpractical/article/details/51034360

需求是计算app在后台的时间,当返回前台时,根据时间差来做相应的操作。

思路是让app中所有的activity都继承baseactivity,然后在baseactivity的onstop()方法中用以下方法判断是否在后台。如果是就开始计时。

在app中做一个标记,记录app是否刚从后台回来。

在onresume()方法中先判断标记是否是刚从后台回来,是的话判断两个时间差跟用户设定的时间是否符合。

BaseActivity中的操作

  @Override
    protected void onStop() {
        super.onStop();
        boolean background = AppIsBackgroundOrForeGroundUtils.isBackground(this);//里面封装的判断前后台的方法。
        if (background) {
            MyApp.isToBackground = true;
            MyApp.currentTime = SystemClock.currentThreadTimeMillis();
        }


    }

    @Override
    protected void onResume() {
        super.onResume();
        if (MyApp.isToBackground) {
            MyApp.isToBackground = false;
            long diffTime = SystemClock.currentThreadTimeMillis() - MyApp.currentTime;

            String setTime = UserInfoCacheSpHelper.getInstance(this).getUserData(Constant.SAFETIME);
            if(setTime==null||"总是".equals(setTime)){
                setTime=0+"";
            }
            long saveTime = Integer.parseInt(setTime) * 60 * 1000;
            if ((diffTime - saveTime) >= 0) {
                finish();
                startActivity(new Intent(this, SplashActivity.class));
            }
        }


    }

判断是否在后台的两种方法:

/**
 * Created by Administrator on 2016/12/2.
 */
public class AppIsBackgroundOrForeGroundUtils {

    /**
     *判断当前应用程序处于前台还是后台
     * 该方法通过RunningTaskInfo类判断,需要在清单文件中添加权限
     * <uses-permission android:name="android.permission.GET_TASKS" />
     */
    public static boolean isApplicationBroughtToBackground(final Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
                return true;
            }
        }
        return false;

    }

    /**
     * 通过RunningAppProcessInfo类判断(不需要额外权限):
     * @param context
     * @return
     */
    public static boolean isBackground(Context context) {

        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                    Log.i("background", appProcess.processName+"后台");
                    return true;
                }else{
                    Log.i("background", appProcess.processName+"前台");
                    return false;
                }
            }
        }
        return false;
    }

}

原文地址:https://www.cnblogs.com/epmouse/p/6051547.html