开发者

android log out help

I have an application that has a user log in and log out. On a 开发者_StackOverflowlog in it tells my DataBase that the user is online. Problem I am having is that when the user doesnt use the app for a while and the processor kills my app is there a method or something where i can run my last piece of code to log them out? I looked at the android life cycle and i cannot use destroy because that only ties with that activity. Thanks!


I found a solution for this - not perfect but worked for me.

1.) Create a service to run in the background which is started when the first activity is created.

2.) Each activity binds to this service so it can "check-in" (i.e. it is alive and onPause) hasn't been called)

3.) In each activity register a broadcast receiver that listens for an intent fired by the service on a regular basis.

4.) On receiving the chech-in intent, it calls a service method which basically lets the service now there is an activity that is still alive (I tent to only respond to the intent if it had windowFocus

5.) If there is a check-in the service sleeps and then re-requests a checkin, if there was no check-in it sleeps for a shorter period of time, before re-requesting a check-in, if none respond then the app logs out. (The reason for the second re-quest when no check-ins were found was to account for issues surrounding check-in during an activity transition, i.e. starting a new activity and closing the current one).

As I said this isn't the nicest way to do it but seems to work for my needs so far.


why can't you use onDestroy method of your activity? if you have a lot of activities, you can create your own base activity class and derive all your activities from this base class.

public abstract class BaseActivity extends Activity {
....
    @Override
    public void onDestroy() {
        super.onDestroy();
        // do your stuff here
    }
}

and thenm create all your activities like this:

public class YourActivity extends BaseActivity {
    ...
}


In AndroidManifest you've got name. Now create

public class MyName extends Application {
}

this is your Application class which is automatically created once user open your app. Now simply override onTerminate() method inside MyName class.

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

You can use your MyName class in every Activity simply with this code:

MyName myName= (MyName) this.getApplication();
myName.logUser(user);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜