i am developing a password storage software. To enter into the application we have to login with valid details.
Now when i press home key, the application should log of automatically and then start all over again from security perspective. It should logout everytime the home key is pre开发者_StackOverflow中文版ssed. How do i do it?
Listening for the home key to be pressed doesn't make much sense on Android since the user can leave your application by a lot of different ways.
What you can do instead is to override onStop() or onPause() depending on your paranoia level, and logout the user in this event.
You can find details on the lifecycle of an Activity in the official documentation : http://developer.android.com/guide/topics/fundamentals.html#actlife
I would suggest you to override these two methods with some debugging inside, try your application and decide in which you should put your logout() code.
@Override
public void onPause() {
super.onPause();
Log.d("MyTestActivity", "onPause()");
}
@Override
public void onStop() {
super.onStop();
Log.d("MyTestActivity", "onStop()");
}
You can implement this by having a listener for "home" button, inside which the action is to something similar: Keep in mind that there is no listner for home. But you can look for a OnStop()
protected void onStop()
{
super.onStop();
deleteFiles(cacheDir);
}
Within the listener call the deleteFiles(cacheDir);
private void deleteFiles(File dir){
if (dir != null){
if (dir.listFiles() != null && dir.listFiles().length > 0){
// RECURSIVELY DELETE FILES IN DIRECTORY
for (File file : dir.listFiles()){
deleteFiles(file);
}
} else {
// JUST DELETE FILE
dir.delete();
}
}
}
Permissions: android.permission.CLEAR_APP_CACHE android.permission.DELETE_CACHE_FILES
Refer:How can I clear the Android app cache?
精彩评论