我想更新我的应用数据库中的表格,这只有在我重新启动应用时才有可能。因此,在用户未与应用程序交互或用户将应用程序置于后台的一段时间后,是否可以以编程方式退出或重新启动Android应用程序。
答案 0 :(得分:0)
只是扩展了这个基本活动而不是AppCompatActivity
public class BaseAppCompactActivity extends AppCompatActivity {
//activity restart this timeout
public static final long DISCONNECT_TIMEOUT = 14400000; // 5 min = 5 * 60 * 1000 ms
private Handler disconnectHandler = new CustomHandler();
private Runnable disconnectCallback = new Runnable() {
@Override
public void run() {
// Perform any required operation on disconnect
finishAffinity();
}
};
public void resetDisconnectTimer(){
disconnectHandler.removeCallbacks(disconnectCallback);
disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
}
public void stopDisconnectTimer(){
disconnectHandler.removeCallbacks(disconnectCallback);
}
@Override
public void onUserInteraction(){
resetDisconnectTimer();
}
@Override
public void onResume() {
System.out.println("Base Activity Resume called");
super.onResume();
resetDisconnectTimer();
}
@Override
public void onStop() {
System.out.println("Base Activity Stop called");
super.onStop();
stopDisconnectTimer();
}
static class CustomHandler extends Handler {
public void handleMessage(Message msg) {
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}