Android detecting if an application entered the background

后端 未结 4 1786
轻奢々
轻奢々 2020-12-05 03:36

I\'m trying to implement some automatic logout code for my Application on Android.

I need to detect if all the activities belonging to an Application have entered th

4条回答
  •  一生所求
    2020-12-05 04:13

    Create an Application class and include in the manifest

    override the following method

    @Override
    public void onTrimMemory(int level) {
            // this method is called when the app goes in background.
            // you can perform your logout service here
            super.onTrimMemory(level);
        }
    

    this is valid of API level 14 and above.

    You can even perform the the logout based on the amount of time the app is in background, which i would suggest is a better option. here is what you can do to create as "session timeout"

    1. save the time stamp in SharedPreferences inside the onTrimMemory(int level) method

    2. on all your activities onStrat() get the time stamp from sharedPref and compare it with current time. based on this you can perform a logout.

    3. and clear the shared pref on onCreat of MyApplication

提交回复
热议问题