簡體   English   中英

如何在應用程序從堆棧中清除時注銷 API,即當應用程序去破壞 android 中的 state 時

[英]How to Logout the API when Application cleared from stack i.e when app goes to destroy state in android

我正在開發銀行應用程序,我想在用戶不活動 state 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;
        }
    }

}

也許這可以幫助你

根據谷歌新的LifecycleObserver機制將幫助你

首先,在您的應用項目中添加以下兩個依賴項
首先,添加項目級gradle

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

然后在App級gradle

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

然后在創建您的應用程序 class 並添加以下邏輯之后(注意:如果您已經創建,請修改並添加以下一些方法)

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
        }
    }
}

最后,如果您還沒有在清單中注冊此 class,請在清單中注冊以設置名稱標簽

<application
        android:name=".MyApplication"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM