当用户完全退出(销毁)应用程序时,是否可以实现会话注销?

时间:2014-09-25 07:14:24

标签: android logout session-timeout

创建了会话注销功能,在以下情况下发生非活动注销: 1.)有一段时间不活动 2.)应用程序被推入后台,当用户恢复应用程序/活动时,活动仍将执行超时注销 3.)屏幕已超时并已进入屏幕保护模式

但是,我想知道当用户退出/将应用程序推入后台后,用户完全终止应用程序时是否可能发生非活动注销。如果可能,有哪些可能的实现?

以下是代码段:

(在非活动期间呼叫的功能):

@Override
public void onStop(){
     super.onStop();
     //Timer needs to be stopped when user manually pressed BACK button
     //Prevent a logout when user press BACK button to stop each activity destroyed from being logged
     startTime = System.currentTimeMillis();
     Log.i("RootActivity:onResume()","******startTime=******"+startTime);
     stopDisconnectTimer();
}

//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:onResume()","******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();
        }   
    }
};

//METHOD TO CALL ON RESETDISCONNECT WHEN USER ACTIVITY RESUMES
public void resetDisconnectTimer(){
    MyBaseActivity.disconnectHandler.removeCallbacks(disconnectCallback);
    MyBaseActivity.disconnectHandler.postDelayed(disconnectCallback, Inactivity_Timeout);
}

//METHOD TO CALL ON STOPDISCONNECT WHEN USER PRESS BACK BUTTON
public void stopDisconnectTimer(){
    MyBaseActivity.disconnectHandler.removeCallbacks(disconnectCallback);
}

2 个答案:

答案 0 :(得分:0)

将您的注销代码放在onDestroy方法中。当用户销毁应用程序时,将调用此方法 -

@Override
protected void onDestroy() {
    super.onDestroy();
    //Log out the user
}

答案 1 :(得分:0)

采用单独的类并扩展应用程序 如下

// *********** ************************

   public class AppController extends Application{


  public static final String TAG1 = "SCREEN";
  ConnectionDetector cd;
  Boolean isInternetPresent = false;     
  private BroadcastReceiver scrOnReceiver;
    private BroadcastReceiver scrOffReceiver;
    private IntentFilter scrOnFilter;
    private IntentFilter scrOffFilter;
public static final String TAG = AppController.class.getSimpleName();

private RequestQueue mRequestQueue;

private static AppController mInstance;

@Override
public void onCreate() {
    super.onCreate();

 // creating connection detector class instance
    cd = new ConnectionDetector(getApplicationContext());
    // get Internet status
    isInternetPresent = cd.isConnectingToInternet();

    new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent) {
              if (isInternetPresent) {
                  Toast.makeText(getApplicationContext(), "internet connection", Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(getApplicationContext(), "Please check the internet connection", Toast.LENGTH_SHORT).show();
                    Intent intent1=new Intent(AppController.this,LoginActivity.class);
                    intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    Log.d("***********", "logout");
                }
        }

    };





    //Fabric.with(this, new Crashlytics());
    mInstance = this;

    scrOnReceiver = new BroadcastReceiver() { 
        @Override 
        public void onReceive(Context context, Intent intent) { 
            Log.d(TAG1, "SCREEN ON"); 
            timer.cancel();
        } 
    };

    scrOnFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); 

    scrOffReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG1, "SCREEN OFF");
            timer.start();



        }
    };

    scrOffFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);

    registerReceiver(scrOnReceiver, scrOnFilter);
    registerReceiver(scrOffReceiver, scrOffFilter);
}

public static synchronized AppController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}

public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}



CountDownTimer timer = new CountDownTimer(1*60*1000, 1000) {

    public void onTick(long millisUntilFinished) {
       //Some code
        Log.d("timer", "****"+millisUntilFinished);

    }

    public void onFinish() {
       //Logout
    //Intent intent=new Intent(AppController.this,TicketActivity.class);
        //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        Log.d("***********", "logout");
        try {
             Intent intent = new Intent(AppController.this,ArchitectureActivity.class);
             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
           //  startActivity(intent);        
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
 };
@Override
public void onTerminate() {
    super.onTerminate();

    unregisterReceiver(scrOnReceiver);
    unregisterReceiver(scrOffReceiver);
}

}