gpt4 book ai didi

android - 检测应用程序何时打开/恢复

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:22 26 4
gpt4 key购买 nike

我想在每次应用程序打开/关闭时通过服务器 checkin 和 checkout 用户,无论是从任务抽屉启动还是恢复。有没有一种方法可以做到这一点,同时避免在每个 Activity 中调用一个函数?

谢谢!

最佳答案

编辑

this answer , matdev 让我注意到通过 ProcessLifeCycleOwner 监听应用程序生命周期事件的更现代的方法。参见 https://developer.android.com/topic/libraries/architecture/lifecycle

因此,为了更好地组织所需的 session 管理功能,应使用以下结构。在 MyApplicationonCreate 中注册 SessionTracker。然后将与跟踪用户 session 相关的功能隔离到 SessionTracker 类中。

首先添加到你的build.gradle

dependencies {
implementation "android.arch.lifecycle:extensions:1.1.1"
}

然后,执行以下操作:

public class MyApplication extends Application {  

@Override
public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle().addObserver(SessionTracker.getInstance());
}
}

public class SessionTracker implements LifecycleObserver {
private static SessionTracker sSessionTracker;
private static final long MAX_ACTIVITY_TRANSITION_TIME_MS = 2000; // Time allowed for transitions

private Timer mStopDelayTimer;
private TimerTask mActivityTransitionTimerTask;
private boolean mWasInBackground = true;
private AppSession mAppSession;

public static SessionTracker getInstance() {
if (sSessionTracker == null) {
sSessionTracker = new SessionTracker();
}
return sSessionTracker;
}

private SessionTracker() {
// no-op
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
private void onLifecycleStop() {
submitAppSession(appSession);
}

@OnLifecycleEvent(Lifecycle.Event.ON_START)
private void onLifecycleStart() {
mAppSession = new AppSession();
}

private void submitAppSession(AppSession appSession) {
// TODO submit app session here
}
}

public class AppSession {
/* TODO */
}

上一个答案

d60402提供的答案here以及 Hanno Binder 使用 Application.registerActivityLifecycleCallbacks() 注册 Activity 回调的建议引导我找到这个解决方案。

我扩展了应用程序并将回调注册到 Activity 方法 onPause 和 onStart,如下所示。在这些方法中,一个计时器被启动/停止(一个 Activity 在调用 onPause 时退出,在调用 onStart 时进入一个新 Activity )。当应用程序确定处于后台/前台(真/假)时,标志“wasInBackground”被切换。如果运行 onStart 回调时应用程序处于后台,则调用“appEntered”。如果在 onPause 和 onStart 回调之间传递的时间大于指定时间(为 Activity 转换提供足够的时间),则在应用 session 被视为完成时调用“appExited”。

public class MyApplication extends Application {

public static final String LOG_TAG = "MyApp";

public boolean wasInBackground = true;

private AppSession appSession;
private Timer mActivityTransitionTimer;
private TimerTask mActivityTransitionTimerTask;
private final long MAX_ACTIVITY_TRANSITION_TIME_MS = 2000; // Time allowed for transitions

Application.ActivityLifecycleCallbacks activityCallbacks = new Application.ActivityLifecycleCallbacks() {

@Override
public void onActivityResumed(Activity activity) {

if (wasInBackground) {
//Do app-wide came-here-from-background code
appEntered();
}
stopActivityTransitionTimer();
}

@Override
public void onActivityPaused(Activity activity) {
startActivityTransitionTimer();
}

...

};

@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(activityCallbacks);
}

public void startActivityTransitionTimer() {
this.mActivityTransitionTimer = new Timer();
this.mActivityTransitionTimerTask = new TimerTask() {
public void run() {
// Task is run when app is exited
wasInBackground = true;
appExited();
}
};

this.mActivityTransitionTimer.schedule(mActivityTransitionTimerTask,
MAX_ACTIVITY_TRANSITION_TIME_MS);
}

public void stopActivityTransitionTimer() {
if (this.mActivityTransitionTimerTask != null) {
this.mActivityTransitionTimerTask.cancel();
}

if (this.mActivityTransitionTimer != null) {
this.mActivityTransitionTimer.cancel();
}

this.wasInBackground = false;
}

private void appEntered() {
Log.i(LOG_TAG, "APP ENTERED");

appSession = new AppSession();
}

private void appExited() {
Log.i(LOG_TAG, "APP EXITED");

appSession.finishAppSession();

// Submit AppSession to server
submitAppSession(appSession);
long sessionLength = (appSession.getT_close() - appSession.getT_open())/1000L;
Log.i(LOG_TAG, "Session Length: " + sessionLength);
}

关于android - 检测应用程序何时打开/恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29924488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com