我遇到的问题是警报对话框每隔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;
}
答案 0 :(得分:1)
你可以这样做:
保持布尔值isLoggedIn
最初,将其声明为
isLoggedIn = false;
当用户成功登录时,您可以设置该值true
。
稍后,在显示警告对话框之前,请检查布尔值。
if(isLoggedIn){
//show alert
}
else{
//do nothing
}
希望它有所帮助。
答案 1 :(得分:1)
从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();
}
}
}
};