【问题标题】:Is it possible to implement a session logout when the user has totally exit(destroy) the app?当用户完全退出(销毁)应用程序时,是否可以实现会话注销?
【发布时间】:2014-09-25 07:14:24
【问题描述】:

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

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

这里是sn-p的代码:

(在非活动期间调用的功能):

@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);
}

【问题讨论】:

    标签: android logout session-timeout


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢伙计!!我真傻,没想到
    • OnDestroy() 发生在每个活动以及整个应用程序中。如何让 onDestroy 仅对整个应用起作用,而不仅仅是对活动起作用?
    【解决方案2】:

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

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

       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);
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多