[英]How to display alert dialog in activity after user resume app
手頭的問題:
當應用程序推到后台或屏幕超時時,應用程序本身將執行自動會話超時注銷。 因此,為了促進更好的用戶交互,我在“警報”對話框中添加了通知用戶該帳戶由於會話超時而被注銷,並要求他們再次重新登錄。 會話超時方法正常正常運行。
但是,將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
}
試試這個家,我假設你的實現是好的。 因此,僅解決您的問題即可..大聲笑以這種方式實施,這樣我們會像這樣(從這里開始整理)
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放置在alert對話框中,因此當用戶單擊“確定”登錄時,它將帶他進入登錄屏幕,如您所願,然后讓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 ..讓我知道它是否好
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.