如何在应用程序进入android后台后5分钟后自动注销

问题描述 投票:0回答:1

我在5分钟活动后正在进行银行应用程序应该进入登录界面,我引用了很多链接并实现了,当应用程序在前台时它正在工作,我正在调用计时器并在5分钟后检查我被重定向到登录屏幕。

但是当应用程序在后台如何处理时,如果我登录相同的计时器并重定向到登录页面意味着自动打开它。它不应该打开。

在背景本身,它应该移动到登录屏幕右侧。请帮我解决我的问题。

我提到了这个链接:https://gist.github.com/dseerapu/b768728b3b4ccf282c7806a3745d0347

android session logout
1个回答
0
投票

应用程序处于后台或前台状态后,其中一个应用程序成功地在5分钟后自动注销。

要创建5分钟的自动注销,请首先创建所有类,如下所示:

ApplockManager

public class ApplockManager {
    private static ApplockManager instance;
    private DefaultApplock currentAppLocker;

    public static ApplockManager getInstance() {
        if (instance == null) {
            instance = new ApplockManager();
        }
        return instance;
    }

    public void enableDefaultAppLockIfAvailable(Application currentApp) {
        currentAppLocker = new DefaultApplock(currentApp);
    }

    public void startWaitThread(Context context){
        currentAppLocker.startWaitThread(context);
    }

    public void updateTouch(){
        currentAppLocker.updateTouch();
    }

    public void setStopTrue(){
        currentAppLocker.setStopTrue();
    }

    public void setStopFalse(){
        currentAppLocker.setStopFalse();
    }

}

DefaultApplock

public class DefaultApplock implements Application.ActivityLifecycleCallbacks {

        final String TAG = DefaultApplock.class.getSimpleName();

        private Application mCurrentApp;

        private long WAIT_TIME = 5 * 60 * 1000;
        private Waiter waiter;
        private Date mLostFocusDate;

        public DefaultApplock(Application app) {
            super();
            mCurrentApp = app;

            //Registering Activity lifecycle callbacks
            mCurrentApp.unregisterActivityLifecycleCallbacks(this);
            mCurrentApp.registerActivityLifecycleCallbacks(this);
        }

        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {
            // for UserInactivity

            // for Screen lock
            if (shouldShowUnlockScreen()) {
                Log.d(TAG, "time over");

                Intent intent = new Intent(activity.getApplicationContext(), SplashActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                Log.d(TAG, "changing mLostFocus to null");
                mLostFocusDate = null;
                activity.getApplicationContext().startActivity(intent);

            }
        }

        private boolean shouldShowUnlockScreen() {
            Boolean isvalid = false;
            if (mLostFocusDate == null) {
                isvalid = false;
            } else {
                Log.d(TAG, "Timeout ->"+timeSinceLocked());
                if (timeSinceLocked() >= (WAIT_TIME/1000)) {
                    isvalid = true;
                } else {
                    mLostFocusDate = null;
                }
            }
            Log.d(TAG, isvalid.toString());
            return isvalid;
        }

        private int timeSinceLocked() {
            return Math.abs((int) ((new Date().getTime() - mLostFocusDate.getTime()) / 1000));
        }


        @Override
        public void onActivityPaused(Activity activity) {
            /*if(waiter!=null) {
                waiter.stopThread();
            }*/
            mLostFocusDate = new Date();
        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }

        public void startWaitThread(Context context){
            /*if(waiter!=null) {
                waiter.stopThread();
            }*/
            waiter = new Waiter(context, WAIT_TIME);
            waiter.start();
        }

        public void updateTouch() {
            if(waiter!=null) {
                waiter.touch();
            }
            mLostFocusDate = new Date();
        }

        public void setStopTrue() {
            if(waiter!=null) {
                waiter.setStopTrue();
            }
        }

        public void setStopFalse() {
            if(waiter!=null) {
                waiter.setStopFalse();
            }
        }

    }

服务员

public class Waiter extends Thread
{
    private static final String TAG=Waiter.class.getName();
    private long lastUsed;
    private long period;
    private boolean stop = false;
    private Context mContext;
    SessionManager session;

    public Waiter(Context context,long period) {
        this.period=period;
        stop=false;
        mContext = context;
        session = new SessionManager(context.getApplicationContext());
    }

    public void run() {
        long idle=0;
        this.touch();
        do
        {
            idle = System.currentTimeMillis() - lastUsed;
            if(idle > period)
            {
                idle=0;
                // Perform Your desired Function like Logout or expire the session for the app.
                stopThread();
            }
        }
        while(!stop);
        Log.d(TAG, "Finishing Waiter thread");
    }

    public synchronized void touch() {
        lastUsed = System.currentTimeMillis();
    }

    public synchronized void setStopTrue() {
        stop = true;
    }

    public synchronized void setStopFalse() {
        stop = false;
    }

    public synchronized void forceInterrupt() {
        this.interrupt();
    }

    public synchronized void setPeriod(long period)
    {
        this.period=period;
    }

    public synchronized void stopThread() {
        stop = true;
        session.logoutUserInBackgroundOrForeground(mContext);
    }

    public synchronized void startThread() {
        stop = false;
    }

}

SessionManager

public class SessionManager {
    // Shared Preferences
    SharedPreferences pref;

    // Editor for Shared preferences
    Editor editor;

    // Context
    Context _context;

    // Shared pref mode
    int PRIVATE_MODE = 0;

    // Sharedpref file name
    private static final String PREF_NAME = "MyAutoLogoutAppPref";

    // All Shared Preferences Keys
    private static final String IS_LOGIN = "IsLoggedIn";

    // Constructor
    public SessionManager(Context context) {
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

    /**
     * Create login session
     */
    public void createLoginSession() {
        // Storing login value as TRUE
        editor.putBoolean(IS_LOGIN, true);
        // commit changes
        editor.commit();
    }

    public void setIsLogin(boolean login) {
        editor.putBoolean(IS_LOGIN, login);
        editor.commit();
    }

    public boolean isLoggedIn() {
        return pref.getBoolean(IS_LOGIN, false);
    }

    public void logoutUserInBackgroundOrForeground(Context context) {
        setIsLogin(false);
        editor.clear();
        editor.commit();

        Intent i = new Intent(_context, LoginActivity.class);
        // Closing all the Activities
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        // Add new Flag to start new Activity
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // Staring Login Activity
        _context.startActivity(i);
    }

}

MyAutoLogoutApp

public class MyAutoLogoutApp extends Application {
    public static MyAutoLogoutApp myAutoLogoutApp;

    @Override
    public void onCreate() {
        super.onCreate();
        myAutoLogoutApp = this;
        ApplockManager.getInstance().enableDefaultAppLockIfAvailable(this);
        ApplockManager.getInstance().startWaitThread(myAutoLogoutApp);
    }

    public void touch() {
        ApplockManager.getInstance().updateTouch();
    }

    public void setStopTrue() {
        ApplockManager.getInstance().setStopTrue();
    }

    public void setStopFalse() {
        ApplockManager.getInstance().setStopFalse();
        ApplockManager.getInstance().startWaitThread(MyAutoLogoutApp.myAutoLogoutApp);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

BaseActivity

public class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onUserInteraction() {
        super.onUserInteraction();
        MyAutoLogoutApp.myAutoLogoutApp.touch();
    }

}

最后,您的每个活动都必须扩展BaseActivity,因此当用户触摸任何位置时,onUserInteraction()方法将触发并且计时器将重置,并且它将适用于后台和前景场景。

© www.soinside.com 2019 - 2024. All rights reserved.