【问题标题】:How to display alert dialog in activity after user resume app用户恢复应用程序后如何在活动中显示警报对话框
【发布时间】:2014-09-18 15:14:26
【问题描述】:

手头的问题:

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

但是,当我将 AlertDialog 方法放在 onResume() 中时,我无法获得想要的结果。下面是已经完成的代码的sn-p。任何人都可以帮忙,以便在用户恢复活动时显示 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
}

【问题讨论】:

  • 你把你的方法命名为和类(AlertDialog)一样吗??
  • @SagarPilkhwal 我运行 Logcat 时它没有登录!!我实际上不能将 alertdialog 方法放在循环条件原因之外,这将导致函数失败,因为用户将无法登录并且 alertdialog 消息不断显示
  • @SagarPilkhwal,一点也不。还在尝试
  • @SagarPilkhwal 是的,idlelogout 方法在 if (secInt > Inactivity_Timeout){} 循环内工作
  • @SagarPilkhwal 所以我可以知道你在暗示什么吗?我还是没听懂。

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


【解决方案1】:

试试这个家,我假设你的实施很好。因此,仅根据您的问题解决您的问题..lol 使用这种方式实现它,所以我们会是这样的(编辑从这里开始)

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 中,因此当用户单击确定登录时,它会将他带到登录屏幕,就像你想要的那样,然后让 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。从您的空闲注销..好的.. 让我知道它是否好

【讨论】:

  • 好的,我会尽力告诉你的!!谢谢你的建议
  • 想确认一下。我将不得不将第二个 alertDialog() 方法的名称更改为其他名称,对吗?否则,它实际上是不正确的。其次,在 alertDialog.create();,它声明需要添加演员表?第三,alertDialog().show();,表示不能解析或者不是字段,这个方法应该放在if (secInt > Inactivity_Timeout){条件里面,是吗?
  • 是的-第一个示例是我在第二个示例中所做的一般示例,因此复制粘贴第二个示例以替换您的 public void AlertDialog(){} 方法获取它..并显示它在你的简历中..我没有明白你在第二个任务中的意思。并在您的第三个任务中将构建器方法与您的 onresume..@ErnestLee 放在同一类中
  • 为了更安全,将 alertdialog 放在 IdleLogout(){} 方法中,这样当注销完成后,会弹出登录对话框..但我知道您的实现与 alertdialog 一起工作..
  • 整个会话注销方法正在工作,是我是一个问题的警报对话框。
猜你喜欢
  • 2017-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
  • 1970-01-01
  • 2017-08-21
相关资源
最近更新 更多