这是我的代码
public class LogoutService extends Service {
public static CountDownTimer timer;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
timer = new CountDownTimer(1 * 60 * 1000, 1000) {
public void onTick(long millisUntilFinished) {
//Some code
Log.v("Timer::", "Service Started");
}
public void onFinish() {
Log.v("Timer::", "Call Logout by Service");
// Code for Logout
stopSelf();
}
};
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
答案 0 :(得分:1)
您可以使用默认方法找到用户互动:
@Override public void onUserInteraction() {
super.onUserInteraction();
}
使用此方法检查用户交互,如果在时间限制后未找到任何交互,请调用注销方法。
找到更多信息答案 1 :(得分:0)
创建一个需要由所有活动扩展的BaseActivity类。
public class MyBaseActivity extends Activity {
public static final long DISCONNECT_TIMEOUT = 900000; // 15 min = 15 * 60 * 1000 ms
private Handler disconnectHandler = new Handler(){
public void handleMessage(Message msg) {
}
};
private Runnable disconnectCallback = new Runnable() {
@Override
public void run() {
// Perform any required operation on disconnect
// Logout from app
}
};
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() {
super.onResume();
resetDisconnectTimer();
}
@Override
public void onStop() {
super.onStop();
stopDisconnectTimer();
}
}
在其他方面,这也可行。
public class MyBaseActivity extends Activity {
public static final int IDLE_DELAY_MINUTES = 15; // 15 min
@Override
public void onUserInteraction() {
super.onUserInteraction();
delayedIdle(IDLE_DELAY_MINUTES);
}
Handler _idleHandler = new Handler();
Runnable _idleRunnable = new Runnable() {
@Override
public void run() {
//handle your IDLE state
// Logout from app
}
};
private void delayedIdle(int delayMinutes) {
_idleHandler.removeCallbacks(_idleRunnable);
_idleHandler.postDelayed(_idleRunnable, (delayMinutes * 1000 * 60));
}
}
答案 2 :(得分:0)
您应该使用Handler
。像
android.os.Handler handler = new android.os.Handler(); //declear them globly
Runnable runnable=null;
在Runnable
内初始化onCreate()
,如
runnable = new Runnable() {
@Override
public void run() {
finish();
}
};
在代码中复制这两种方法
void start() {
handler.postDelayed(runnable, TimeUnit.MINUTES.toMillis(15));
}
void stop(){
handler.removeCallbacks(runnable);
}
覆盖onUserInteraction()
的{{1}}方法并致电Activity
和start()
stop()
在可运行的初始化之后,不要忘记从 @Override
public void onUserInteraction() {
super.onUserInteraction();
stop();
start();
}
调用start()方法。
它的onCreate()
方法会在用户处于非活动状态后15分钟后调用,在那里执行您的任务(您希望完成run()
或其他内容)
<强> ////////// =====更新===== \\\\\\\\\ 强>
将此Activity
粘贴到您的代码中
BaseActivity
现在扩展import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.util.concurrent.TimeUnit;
public class BaseActivity extends AppCompatActivity {
private static android.os.Handler handler = new android.os.Handler();
private static Runnable runnable = null;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (handler == null) {
handler = new Handler();
} else {
handler.removeCallbacks(runnable);
}
if (runnable == null)
runnable = new Runnable() {
@Override
public void run() {
//do your task here
}
};
start();
}
@Override
public void onUserInteraction() {
super.onUserInteraction();
stop();
start();
}
void start() {
handler.postDelayed(runnable, TimeUnit.MINUTES.toMillis(15));
}
void stop() {
handler.removeCallbacks(runnable);
}
}
除BaseActivity
之外的所有Activity
,或者像这样。
AppCompatActivity
您是否在public class HomeActivity extends BaseActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
}
方法内执行任务,我建议您从run()
方法启动另一个Activity
并清除活动堆栈,因为run()
仅适用于1 { {1}}。