问题描述
用户开始MYAPP和日志中。选择会话超时为5分钟。做一些操作上的应用程序......(全在前台)现在用户带来MYAPP背景,并开始了一些其他的应用程序 ---->倒计时开始5分钟后注销用户或用户关闭屏幕, ---->倒计时开始5分钟后注销用户
我的问题:
我希望这种相同的行为,即使该应用程序是在前台,但用户不与经过较长时间的应用程序进行交互说6-7分钟......假设屏幕上的所有时间......我要检测一种用户不活动(与应用程序,即使该应用程序是在前台没有交互),并开始踢我的倒数计时器。
在此先感谢。
MyApplciation扩展应用(){
私人诠释lastInteraction;
私人布尔isScreenOff = FALSE;
公共无效的onCreate(){
super.onCreate();
......
startUserInactivityDetectThread(); //开始探测活动的线程
新ScreenReceiver(); //创建接收SCREEN_OFF和SCREEN_ON广播封邮件从设备。
}
公共无效startUserInactivityDetectThread(){
新主题(新的Runnable(){
公共无效的run(){
而(真){
视频下载(15000); //检查每一个15秒的闲置
如果(isScreenOff || getLastInteractionTime>!120000 || isInForeGrnd)
{
// ......意味着用户处于不活动状态一段
//你做你的东西,如注销用户
}
}
})。开始();
}
众长getLastInteractionTime(){
返回lastInteractionTime;
}
众长setLastInteractionTime(INT lastInteraction){
lastInteractionTime = lastInteraction;
}
私有类ScreenReceiver扩展的BroadcastReceiver {
保护ScreenReceiver(){
//注册接收处理屏幕上,屏幕关闭逻辑
IntentFilter的过滤器=新的IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(这一点,过滤器);
}
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
如果(intent.getAction()。等于(Intent.ACTION_SCREEN_OFF)){
isScreenOff = TRUE;
}否则,如果(intent.getAction()。等于(Intent.ACTION_SCREEN_ON)){
isScreenOff = FALSE;
}
}
}
}
isInForeGrnd ===>逻辑这里没有显示,因为它是出了问题的范围
User start MyAPP and logs in.Selects Session Timeout to be 5 mins.Does some operations on the app.......(all in foreground)Now User bring Myapp to background and starts some other app ----> Count down timer starts and logs out user after 5 minsOR user turns the screen OFF ----> Count down timer starts and logs out user after 5 mins
My Question:
I want this same behavior even when the app is in the foreground but user doesn't interact with the app for a long-time say 6-7 mins... Assume the screen is ON all the time... I want to detect kind of USER INACTIVITY (No interaction with app even though the app is in the foreground) and kick start my count down timer.
Thanks in advance.
MyApplciation extents Application(){
private int lastInteraction;
private Boolean isScreenOff = false;
public void onCreate(){
super.onCreate();
......
startUserInactivityDetectThread(); // start the thread to detect inactivity
new ScreenReceiver(); // creating receive SCREEN_OFF and SCREEN_ON broadcast msgs from the device.
}
public void startUserInactivityDetectThread(){
new Thread(new Runnable() {
public void run() {
while(true) {
Thread.sleep(15000); // checks every 15sec for inactivity
if(isScreenOff || getLastInteractionTime > 120000 || !isInForeGrnd)
{
//...... means USER has been INACTIVE over a period of
// and you do your stuff like log the user out
}
}
}).start();
}
public long getLastInteractionTime() {
return lastInteractionTime;
}
public long setLastInteractionTime(int lastInteraction) {
lastInteractionTime = lastInteraction;
}
private class ScreenReceiver extends BroadcastReceiver {
protected ScreenReceiver() {
// register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(this, filter);
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
isScreenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
isScreenOff = false;
}
}
}
}
isInForeGrnd ===> logic is not shown here as it is out of scope of the question
这篇关于如何检测的Android用户不活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!