我遇到的问题是,每隔2分钟,警报对话框就会不断出现;无论登录/注销状态如何。
如果用户已登录,则将在2分钟后显示警告对话框,并且由于不活动而将用户注销,并且该用户将被重定向到登录页面(正确功能)。但是,当用户最初未登录时(错误功能),警报对话框仍会在2分钟后显示。
因此,当用户未登录时,如何确保AlertDialog在规定的不活动时间后不显示?
以下是相关的代码段:
检查无效性的方法:
//METHOD USED FOR INACTIVITY LOGOUT
//EMPLOY THE HANDLER METHOD FOR OCCURANCE OF FUTURE FUNCTION: DISCONNECTHANDLER
public static class MyBaseActivity extends Activity {
public static Handler disconnectHandler = new Handler(){
public void handleMessage(Message msg){
}
};
}
private Runnable disconnectCallback= new Runnable(){
@Override
public void run(){
//Get the Resume Time & get difference in Time for Logout
long endTime= System.currentTimeMillis();
Log.i("RootActivity:Runnabe()","******endTime=******"+endTime);
long diff = endTime - startTime;
long secInt = (diff /1000); //conversion of milliseconds into seconds
Log.i("RootActivity:onRun()","******sectInt=******"+secInt);
if (secInt > Inactivity_Timeout){// SET EXIT SCREEN INTERVAL LOGOUT
IdleLogout();
}
}
};
登出方法:
//LOGOUT METHOD & CLEARING OF SHARED PREFERENCE CREDENTIALS
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
Log.i("RootActivity:IdleLogout()","******loginButton is set******");
setloginButton(); // Change logout button to login
RootActivity.alertDialog(RootActivity.this,getCustomIntent(RewardsActivity.class)).create().show();
}
调用警报对话框的方法:
static Builder alertDialog(final Activity act,final Intent yourintent){
Log.i("RootActivity:alertDialog","******Session Logout Info******");
AlertDialog.Builder alertDialog = new AlertDialog.Builder(act);
alertDialog.setTitle("SESSION LOGOUT NOTICE");
alertDialog.setMessage("You Have Been Logged Out Due To Inactivity." +
"Please Login To Access Your Profile.");
alertDialog.setCancelable(false);
//SETTING OF OK BUTTON
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.startActivity(yourintent);//Return page to PROPERTYACTIVITY
}
}); return alertDialog;
}
检查用户是否已登录的方法:
public void checkLogin(){
//CONDITION TO CHECK IF USER IS LOGIN, IF TRUE, CALL METHOD IdleLogout()
if(getSharedPreferences(getString(R.string.pref_current_user), MODE_PRIVATE).getString(
getString(R.string.pref_password), "") != ""){
IdleLogout();
}return;
}
IdleLogout()
从拨打电话时,请检查是否存在凭据disconnectCallback
。如果存在凭据,则仅致电IdleLogout()
,否则不致电。如果我正确理解了您的代码,那么它将停止弹出警报对话框
private Runnable disconnectCallback= new Runnable(){
@Override
public void run(){
//Get the Resume Time & get difference in Time for Logout
long endTime= System.currentTimeMillis();
Log.i("RootActivity:Runnabe()","******endTime=******"+endTime);
long diff = endTime - startTime;
long secInt = (diff /1000); //conversion of milliseconds into seconds
Log.i("RootActivity:onRun()","******sectInt=******"+secInt);
if (secInt > Inactivity_Timeout){// SET EXIT SCREEN INTERVAL LOGOUT
if(getSharedPreferences(getString(R.string.pref_current_user), MODE_PRIVATE).getString(
getString(R.string.pref_password), "") != ""){
IdleLogout();
}
}
}
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句