这是一个完整的解决方案,可以在几分钟(例如 3 分钟)后处理用户不活动。解决了App在后台超时Activity跳到前台等常见问题。
首先,我们创建一个所有其他 Activity 都可以扩展的 BaseActivity。
这是 BaseActivity 代码。
package com.example.timeout;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import javax.annotation.Nullable;
public class BaseActivity extends AppCompatActivity implements LogoutListener {
private Boolean isUserTimedOut = false;
private static Dialog mDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((TimeOutApp) getApplication()).registerSessionListener(this);
((TimeOutApp) getApplication()).startUserSession();
}
@Override
public void onUserInteraction() {
super.onUserInteraction();
}
@Override
protected void onResume() {
super.onResume();
if (isUserTimedOut) {
//show TimerOut dialog
showTimedOutWindow("Time Out!", this);
} else {
((TimeOutApp) getApplication()).onUserInteracted();
}
}
@Override
public void onSessionLogout() {
isUserTimedOut = true;
}
public void showTimedOutWindow(String message, Context context) {
if (mDialog != null) {
mDialog.dismiss();
}
mDialog = new Dialog(context);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.dialog_window);
mDialog.setCancelable(false);
mDialog.setCanceledOnTouchOutside(false);
TextView mOkButton = (TextView) mDialog.findViewById(R.id.text_ok);
TextView text_msg = (TextView) mDialog.findViewById(R.id.text_msg);
if (message != null && (!TextUtils.isEmpty(message)) && (!message.equalsIgnoreCase("null"))) {
text_msg.setText(message);
}
mOkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mDialog != null){
mDialog.dismiss();
Intent intent = new Intent(BaseActivity.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
}
});
if(!((Activity) context).isFinishing())
{
//show dialog
mDialog.show();
}
}
}
接下来,我们为我们的“Logout Listener”创建一个接口
package com.example.timeout;
public interface LogoutListener {
void onSessionLogout();
}
最后,我们创建一个扩展“应用程序”的 Java 类
package com.example.timeout;
import android.app.Application;
import java.util.Timer;
import java.util.TimerTask;
public class TimeOutApp extends Application {
private LogoutListener listener;
private Timer timer;
private static final long INACTIVE_TIMEOUT = 180000; // 3 min
public void startUserSession () {
cancelTimer ();
timer = new Timer ();
timer.schedule(new TimerTask() {
@Override
public void run() {
listener.onSessionLogout ();
}
}, INACTIVE_TIMEOUT);
}
private void cancelTimer () {
if (timer !=null) timer.cancel();
}
public void registerSessionListener(LogoutListener listener){
this.listener = listener;
}
public void onUserInteracted () {
startUserSession();
}
}
注意:不要忘记将“TimeOutApp”类添加到清单文件中的应用程序标签
<application
android:name=".TimeOutApp">
</application>