Android 注销帮助

发布于 2024-10-20 22:56:27 字数 156 浏览 1 评论 0原文

我有一个有用户登录和注销的应用程序。登录时它告诉我的数据库用户在线。我遇到的问题是,当用户一段时间不使用该应用程序并且处理器杀死我的应用程序时,是否有一种方法或某种方法可以运行我的最后一段代码来将它们注销?我查看了 android 生命周期,我不能使用 destroy,因为它只与该活动相关。谢谢!

I have an application that has a user log in and log out. On a log 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两相知 2024-10-27 22:56:27

我找到了一个解决方案 - 并不完美,但对我有用。

1.) 创建一个在后台运行的服务,该服务在创建第一个活动时启动。

2.) 每个活动都绑定到此服务,以便它可以“签入”(即它处于活动状态并且 onPause)尚未被调用)

3.) 在每个活动中注册一个广播接收器,用于侦听由服务触发的意图定期进行。

4.) 在收到签入意图时,它会调用一个服务方法,该方法基本上让服务现在有一个仍然活动的活动(我倾向于仅在具有 windowFocus

5 的情况下响应该意图)。签入服务会休眠,然后重新请求签入,如果没有签入,它会在重新请求签入之前休眠较短的时间,如果没有响应,则应用程序会注销。 (未找到签到时进行第二次请求的原因是为了解决活动转换期间签到的问题,即开始一项新活动并关闭当前活动)。

正如我所说,这不是最好的方法,但到目前为止似乎可以满足我的需求。

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.

握住我的手 2024-10-27 22:56:27

为什么你不能使用 Activity 的 onDestroy 方法?如果您有很多活动,您可以创建自己的活动基类并从该基类派生所有活动。

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

然后创建你的所有活动,如下所示:

public class YourActivity extends BaseActivity {
    ...
}

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 {
    ...
}
终陌 2024-10-27 22:56:27

在 AndroidManifest 中你有名字。现在创建

public class MyName extends Application {
}

这是您的应用程序类,一旦用户打开您的应用程序,就会自动创建该类。现在只需重写 MyName 类中的 onTerminate() 方法即可。

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

您只需使用以下代码即可在每个 Activity 中使用 MyName 类:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文