简体   繁体   English

在后台运行15分钟后完成申请

[英]Finish application after 15 minutes in background

How to finish application after 15 minutes, when user minimized? 如何在用户最小化后15分钟后完成申请?

When user exit from the application without finish it, then user turn back after some time network of application is not working. 当用户从应用程序退出而没有完成它时,用户在一段时间的应用程序网络不工作后返回。 For work need to restart the application. 对于工作需要重新启动应用程序。

I want to trigger when the app becomes at the background and then with timer finish application. 我想在应用程序变为后台时触发,然后使用计时器完成应用程序。

Or how to know when the application is paused, not an activity? 或者如何知道应用程序何时暂停,而不是活动?

Lifecycle of the application. 应用程序的生命周期。

Solution: 解:

Make one BaseActivity which is parent of all of your activities . 创建一个BaseActivity ,它是您所有活动的父级。

public class BaseActivity extends AppCompatActivity {
    private static Thread t = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();

        if (t != null) {
            try {
                if (t.isAlive()) {
                    t.interrupt();
                    t.join();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            t = null;
        }
    }

    @Override
    public void onPause() {
        super.onPause();

        t = new Thread() {
            public void run() {
                try {
                    sleep(15 * 60 * 1000);
                     finishAffinity();
                    System.exit(0);
                } catch (InterruptedException e) {
                    return;
                }
            }
        };
        t.start();
    }
}

Now extends BaseActivity in all of your activity class like below sample, 现在扩展所有活动类中的BaseActivity ,如下面的示例,

public class ClassTest extends BaseActivity {
    .....
    .....
}

You need to use ProcessLifecycleOwner to check your app state. 您需要使用ProcessLifecycleOwner来检查您的应用程序状态。 It supports library version 26+. 它支持库版本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);

Here is the dependecy of ProcessLifecycleOwner . 这是ProcessLifecycleOwner的依赖性 For more details visit here 有关详细信息,请访问此处

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM