且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当应用程序进入后台时如何停止可运行?

更新时间:2022-12-27 11:13:17

只需使用onPause()

在活动进入后台但尚未被杀死时被称为活动生命周期的一部分

Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed

@Override
protected void onPause() {
     handler.removeCallbacks(runnable);
     super.onPause();
}

可选

如果要恢复该可运行文件.只需覆盖onResume()回调

If you want to resume that runnable. Just override the onResume() callback

@Override
protected void onResume()
{
      handler.postDelayed(runnable, 5000);
      super.onResume();
}

还删除onCreate()