美文网首页
Android之冷启动和热启动

Android之冷启动和热启动

作者: 因为我的心 | 来源:发表于2021-05-19 16:43 被阅读0次

    一、前言:

    • 冷启动:
      在启动应用时,系统中没有该应用的进程,这时系统会创建一个新的进程分配给该应用;

    • 热启动:
      在启动应用时,系统中已有该应用的进程(例:按back键、home键,应用虽然会退出,但是该应用的进程还是保留在后台)

    二、贴代码:

    1、通用工具类判断是否在前台

    object CommonUtils {
        /**
         * 程序是否在前台运行
         *
         * @return
         */
        fun isAppOnForeground(): Boolean {
            // Returns a list of application processes that are running on the
            val activityManager =
                ChargingElfApplication.instance.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            val packageName: String =
                ChargingElfApplication.instance.getPackageName()
            val appProcesses = activityManager
                .runningAppProcesses ?: return false
            for (appProcess in appProcesses) {
                // The name of the process that this object is associated with.
                if (appProcess.processName == packageName && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    return true
                }
            }
            return false
        }
    
    
        /**
         * 判断APP是否在后台运行的方式
         *  true是在后台运行
         */
        fun isAppOnBackstage(context: Context): Boolean {
            val activityManager =
                context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            val tasks = activityManager.getRunningTasks(1)
            if (!tasks.isEmpty()) {
                val topActivity = tasks.get(0).topActivity
                if (!topActivity?.packageName.equals(context.packageName)) {
                    return true
                }
            }
            return false
        }
    
    }
    

    2、在BaseActivity中统一判断是否是热启动

    public abstract class BaseActivity extends AppCompatActivity {
        /**
         * 默认为true,此页面存活;false进入后台
         */
        boolean isLive = true;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
    
    
        @Override
        protected void onStop() {
            super.onStop();
            boolean appOnForeground = CommonUtils.INSTANCE.isAppOnForeground();
            if (!appOnForeground) {
                //app 进入后台
                isLive = false;
            }
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            if (!isLive) {
                isLive = true;
                //app 从后台唤醒,进入前台(注意:会出现先展示当前页面,有跳转到热启动页面)
                Log.d("LUO", "=====onResume: app 从后台唤醒,进入前台======"); 
                Intent intent = new Intent(this, HotStartActivity.class);
                startActivity(intent);
            }
        }
    }
    

    3、通过万能工具类判断前后台

    //就是这个万能工具 类
    implementation 'com.blankj:utilcodex:1.30.6'
    
      AppUtils.registerAppStatusChangedListener(object : Utils.OnAppStatusChangedListener{
                override fun onBackground(activity: Activity?) {
                    //停止服务
                    stopOnlineTiming()
                    Log.e("统计时长","---onBackground---")
                }
    
                override fun onForeground(activity: Activity?) {
                    //判断服务是否启动
                    startOnlineTiming()
                    Log.e("统计时长","---onForeground---")
                }
            })
    
    

    相关文章

      网友评论

          本文标题:Android之冷启动和热启动

          本文链接:https://www.haomeiwen.com/subject/lwipjltx.html