我有一个android应用程序,需要用户登录凭据才能访问某些数据。我想要做的是当用户决定退出整个APP而不是一个Activity时,如果非活动时间超过20秒,它将调用注销方法。因此,我需要一个运行的代码,它能够检查应用程序在后台,这个程序应该恭维做下面的代码:当应用程序在后台运行注销方法
IdleLogout方法:
public void IdleLogout(){
Log.i("RootActivity:IdleLogout()","******APP LOGGEDOUT******");
SharedPreferences pref = getSharedPreferences(getString(R.string.pref_current_user), MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.clear(); // CLEAR ALL FILEDS
editor.commit(); // COMMIT CHANGES
setloginButton(); // Change logout button to login
RootActivity.alertDialog(RootActivity.this,getCustomIntent(PropertyActivity.class)).create().show();
//startActivity(getCustomIntent(PropertyActivity.class)); //Return page to PROPERTYACTIVITY.
}
WLL利用当前的活动时间
@Override
public void onStop(){
super.onStop();
//get the current time on exit
curDate = new Date();
Log.i("RootActivity:onStop()","******curDate=******"+curDate);
}
将采取活动时用户恢复应用
@Override
protected void onResume() {
super.onResume();
setloginButton();
EnquiryActivity.PROPERTY = 0;
//EDITED FOR SESSION LOGOUT
//Get the Resume Time
resumeDate = new Date();
Log.i("RootActivity:onResume()","******resumeDate=******"+resumeDate);
long diff = resumeDate.getTime() - curDate.getTime();
long secInt = diff/1000 % 60; //conversion of milliseconds into human readable form
Log.i("RootActivity:onResume()","******sectInt=******"+secInt);
if (secInt > Inactivity_Timeout){// SET EXIT SCREEN INTERVAL LOGOUT
IdleLogout();
}
}
当然,我的朋友。这是我成功使用我的应用程序的方式。 – 2014-09-13 04:46:38
你有多个活动?如果是这样,它们中的每一个都应该从在onPause和onResume内部发送广播的自定义创建的活动中扩展。第一个告诉服务开始倒计时,后者告诉服务取消倒计时(如果有的话)。使用Intents进行沟通,因为它是一种快速,安全和简单的方式。 – 2014-09-13 04:48:15
是的,我确实有多项活动,我想澄清一下我对你写的内容的理解,我真的很困惑。你是否指的是我开始实施 - >公共静态类MyApplication扩展应用程序,然后将调用activityResume和activityPaused? – 2014-09-13 04:56:29