如何在用户恢复应用后显示活动中的警报对话框

时间:2014-09-07 10:25:19

标签: android user-interface alertdialog android-alertdialog session-timeout

手上的问题:

当应用程序被推送到后台或屏幕超时时,应用程序本身将执行自动会话超时注销。因此,为了促进更好的用户交互,我已在警报对话框中包含通知用户该帐户由于会话超时而已注销,并要求他们重新登录。会话Timeout方法正常且正常运行。

但是,当我将AlertDialog方法放在onResume()中时,我无法得到想要的结果。下面是已经完成的代码片段。任何人都可以请求帮助,以便在用户恢复活动时显示AlertDialog。

//EDITED FOR SESSION LOGOUT
//WHEN APP IS IN IDLE OR WHEN USER DEVICE SCREEN IS OFF, WILL CALL ON THE METHOD TO LOGOUT USER

@Override
protected void onResume() {
    super.onResume();
    setloginButton();
    EnquiryActivity.PROPERTY = 0;
    //EDITED FOR SESSION LOGOUT
    //Get the Resume Time
    resumeDate = new Date();
    long diff = resumeDate.getTime() - curDate.getTime();
    long secInt = diff / 1000 % 60; //conversion of milliseconds into human readable form
    if (secInt > Inactivity_Timeout){// SET EXIT SCREEN INTERVAL LOGOUT
        IdleLogout();
        AlertDialog();
    }   
}

@Override
public void onUserInteraction() { 
    super.onUserInteraction();
    // TO LOG APP HAS EXITED AND IS PUSHED TO BACKGROUND PROCESS(25/08/2014) 
    .....
}

//Perform Check if User is still Login as an user
public void checkLogin(){
    //CONDITION TO CHECK IF USER IS LOGIN, IF TRUE, CALL METHOD 
 ....
}


 public void startUserInactivityDetectThread(){ 
    new Thread(new Runnable() {

    public void run() {
        // PROVIDING AN INFINITE LOOP WHEN FOLLOWING CONDITIONS ARE TRUE. 
         while(true) {
                  //Perform Thread to check on condition and run idlelogout
        }
       }
       }).start();
       }

public void IdleLogout(){
 // Method to perform logout and erase shared preference credential
}

// TO INFORM USER ON THE STATUS OF LOGOUT WHEN USER RESUMES THE APP
public void AlertDialog() {
    AlertDialog alertDialog = new AlertDialog.Builder(RootActivity.this).create();
    alertDialog.setTitle("SESSION LOGOUT NOTICE");
    alertDialog.setMessage("PLEASE LOGIN TO ACCESS YOUR PROFILE.");
    //SETTING OF OK BUTTON
    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("market://details?id=com.og.ascendas.spacetobe"));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
    });alertDialog.show();// SHOW ALERT MESSAGE
}

1 个答案:

答案 0 :(得分:0)

试试这个家,我假设你的实施是好的。所以只针对你的问题来解决你的问题 用这种方式实现它,所以你会这样(Edditing从这里开始)

static Builder alertDialog(Activity act,final Intent yourintent){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(act);
    alertDialog.setTitle("SESSION LOGOUT NOTICE");
    alertDialog.setMessage("PLEASE LOGIN TO ACCESS YOUR PROFILE.");
    //SETTING OF OK BUTTON
    alertDialog.setPossitiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=com.og.ascendas.spacetobe"));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        act.startActivity(yourintent);//Return page to PROPERTYACTIVITY
      }
   });      
return alertDialog;
}

我将startActivity放在alertdialog中,所以当用户点击ok-关于登录时,它会将他带到登录屏幕,就像你想要的那样,然后让onresume执行它的show();

 @Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    setloginButton();
    EnquiryActivity.PROPERTY = 0;
    //EDITED FOR SESSION LOGOUT
    //Get the Resume Time
    resumeDate = new Date();
    long diff = resumeDate.getTime() - curDate.getTime();
    long secInt = diff / 1000 % 60; //conversion of milliseconds into human readable form
    if (secInt > Inactivity_Timeout){// SET EXIT SCREEN INTERVAL LOGOUT
         IdleLogout();
         RootActivity.alertDialog(RootActitvity.this,getCustomIntent(PropertyActivity.class)).create().show();
        }
}

或将其放入IdleLogout()

public void IdleLogout(){
   // Method to perform logout and erase shared preference credentials
   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(RootActitvity.this,getCustomIntent(PropertyActivity.class)).create().show();
}

对于我从昏昏欲睡的状态下写下的小错误感到抱歉......正确的小错误 如果你把它放在idlelogout()中,不要直接在onresume上调用它,只需调用你的idlelogout,这也是不相关的

editor.remove(getString(R.string.pref_username));   //REMOVE USERNAME FROM STRING
editor.remove(getString(R.string.pref_password));  //REMOVE PASSWROD FROM STRING

因为clear()会这样做..并且还要注意删除startActivity(getCustomIntent(PropertyActivity.class)); //将页面返回到PROPERTYACTIVITY。从你的idlelogout..ok .. 让我知道它的好处