当应用程序从堆栈中清除时(即当应用程序将销毁Android中的状态时)如何注销API

时间:2019-10-07 06:17:16

标签: android session

我正在开发银行应用程序,我想在用户处于非活动状态5分钟后自动注销该应用程序并销毁该应用程序。

5分钟后,我将计时器用于前台服务。

如果不单击注销选项,用户将清除应用程序,这意味着将销毁该应用程序。自动注销的工作方式。给我解决方法。

此代码调用“常见活动中的onuserinteracted方法”

public class MyApp1 extends Application {
    private LogOutListener1 listener;
    private Timer timer;
    private Context context;


    public void startUserSession(Context ctx) {
        this.context = ctx;
        long sessiontime = Prefs.getsessiontime(ctx);
        final long milliseconds = sessiontime * 60000;
        cancelTimer();
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    boolean foreGround = new ForegroundCheckTask().execute(context).get();
                    if (foreGround) {
                        listener.onSessionLogout();
                    } else {
                        long millis = new Date().getTime();
                        Prefs.setcurrenttimestamp(context, millis);
                    }


                } catch (InterruptedException | ExecutionException ignored) {

                }
            }

        }, milliseconds);
    }

    public void cancelTimer() {
        if (timer != null) timer.cancel();
    }

    public void registerSessionListener(LogOutListener1 listener) {
        this.listener = listener;
    }

    public void onUserInteracted() {
        startUserSession(context);
    }

    private static class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> {

        @Override
        protected Boolean doInBackground(Context... params) {
            final Context context = params[0].getApplicationContext();
            return isAppOnForeground(context);
        }

        private boolean isAppOnForeground(Context context) {
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
            if (appProcesses == null) {
                return false;
            }
            final String packageName = context.getPackageName();
            for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
                    return true;
                }
            }
            return false;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

也许这对您有帮助

根据google新的LifecycleObserver机制,您将

首先,将其添加到以下两个依赖项中:在Project your app中
首先,添加项目级别的gradle

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://maven.google.com' }
    }
}

然后在应用程序级别gradle

implementation "android.arch.lifecycle:extensions:1.1.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"

然后在创建您的应用程序类并添加下面的逻辑之后(注意:如果您已经创建了,请修改并在某些方法下面添加)

public class MyApplication extends Application implements LifecycleObserver {


    String strPrafKey = "bi_vrnfX";
    String strKeyTime = "timeKey";
    private String strKeyLogin = "is_login";

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

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onAppBackgrounded() {
        //App in background

        //Store app goes in background time
        SharedPreferences sharedpreferences = getSharedPreferences(strPrafKey, Context.MODE_PRIVATE);
        long currentTimeInSecond = Calendar.getInstance().getTimeInMillis() / 1000;
        sharedpreferences.edit().putLong(strKeyTime, currentTimeInSecond).apply();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onAppForegrounded() {
        // App in foreground

        SharedPreferences sharedpreferences = getSharedPreferences(strPrafKey, Context.MODE_PRIVATE);
        long lastBackgroundTime = sharedpreferences.getLong(strKeyTime, 0); //Take last background time for compare
        long currentTimeInSecond = Calendar.getInstance().getTimeInMillis() / 1000;
        long timeDeffrance = (currentTimeInSecond - lastBackgroundTime) / 60; //Diffrance of last background and current time in minute
        if (timeDeffrance > 5) {
            sharedpreferences.edit().putBoolean(strKeyLogin, false).apply();
            //You can replace this code or data with which you are comparing in your login
        }
    }
}

最后,如果您尚未在清单中注册该类,请在清单中注册以设置名称标签

<application
        android:name=".MyApplication"