Finish application after 15 minutes in background

#1
How to finish application after 15 minutes, when user minimized?

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.
 

Admin

Administrator
Thành viên BQT
#2
Solution:

Make one BaseActivity which is parent of all of your activities.

Mã:
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,
Mã:
public class ClassTest extends BaseActivity {
    .....
    .....
}
 
Top