Java 在后台15分钟后完成应用程序
当用户最小化时,如何在15分钟后完成应用程序 当用户未完成就退出应用程序时,用户会在应用程序的网络不工作一段时间后返回。对于工作,需要重新启动应用程序 我想触发时,应用程序成为在后台,然后与定时器完成应用程序 或者如何知道应用程序何时暂停,而不是活动Java 在后台15分钟后完成应用程序,java,android,Java,Android,当用户最小化时,如何在15分钟后完成应用程序 当用户未完成就退出应用程序时,用户会在应用程序的网络不工作一段时间后返回。对于工作,需要重新启动应用程序 我想触发时,应用程序成为在后台,然后与定时器完成应用程序 或者如何知道应用程序何时暂停,而不是活动 应用程序的生命周期。解决方案: 制作一个BaseActivity,它是所有活动的父级 现在在所有活动类中扩展BaseActivity,如下面的示例所示 public class ClassTest extends BaseActivity {
应用程序的生命周期。解决方案: 制作一个BaseActivity,它是所有活动的父级 现在在所有活动类中扩展BaseActivity,如下面的示例所示
public class ClassTest extends BaseActivity {
.....
.....
}
您需要使用ProcessLifecycleOwner检查应用程序状态。它支持库版本26+
//Check if app is in background
ProcessLifecycleOwner.get().getLifecycle().getCurrentState() ==
Lifecycle.State.CREATED;
//Check if app is in foreground
ProcessLifecycleOwner.get().getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED);
这是对时间的依赖。欲了解更多详情,请访问
当我从mainActivity打开新活动时,onPause方法将运行,并退出应用程序。我认为上述代码不能在活动之间工作否,如果您的
main活动
暂停15分钟,则退出,否则不能。我认为上述代码不应在活动之间工作。我想在后台应用程序使线程变量为static时触发,比如,private static Thread t=null代码>用于测试设置睡眠时间=30秒,如睡眠(30*1000)代码>,所以您不必等待15分钟。如果API低于26怎么办?
//Check if app is in background
ProcessLifecycleOwner.get().getLifecycle().getCurrentState() ==
Lifecycle.State.CREATED;
//Check if app is in foreground
ProcessLifecycleOwner.get().getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED);
dependencies {
def lifecycle_version = "1.1.1"
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
// alternatively - Lifecycles only (no ViewModel or LiveData).
// Support library depends on this lightweight import
implementation "android.arch.lifecycle:runtime:$lifecycle_version"
annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" // use kapt for Kotlin
}