2014-09-25 40 views
0

已创建会话注销功能,可以在以下情况下发生不活动注销: 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); 
} 

回答

0

中的onDestroy方法把你的注销代码。这种方法当用户销毁该应用将被称为 -

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

感谢队友!我傻不想到 – 2014-09-25 08:03:17

+0

的OnDestroy(中)的会为每个活动,也为整个应用程序。我如何让onDestroy只适用于整个应用程序,而不仅仅是活动? – 2014-09-26 02:39:49

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

}