Password protect launch of android application

前端 未结 2 1606
挽巷
挽巷 2021-01-31 06:51

I\'m searching for a way to password protect my android application on launch, i.e. when launching/resuming an activity belonging to my apk-package a password dialog will be sho

相关标签:
2条回答
  • 2021-01-31 06:54

    So this is the solution I stuck with. In my Application class i store a long variable with the system time when an activity was last paused.

    import android.app.Application;
    public class MyApplication extends Application {
        public long mLastPause;
    
        @Override
        public void onCreate() {
            super.onCreate();
            mLastPause = 0;
            Log.w("Application","Launch");
        }
    }
    

    In every onPause-method I update this value to the current time.

    @Override
    public void onPause() {
        super.onPause();
        ((MyApplication)this.getApplication()).mLastPause = System.currentTimeMillis();
    }
    

    And in every onResume I compare it to the current time. If a certain amount of time (currently 5 seconds) has passed my password prompt is shown.

    @Override
    public void onResume() {
        super.onResume();
        MyApplication app = ((MyApplication)act.getApplication());
        if (System.currentTimeMillis() - app.mLastPause > 5000) {
            // If more than 5 seconds since last pause, prompt for password
        }
    }
    
    0 讨论(0)
  • 2021-01-31 07:19

    Subclass Application, and set a variable in there on whether the next time something happens in your app, you should prompt a dialog or not.

    In your Activity, Service, etc... using the Context you receive, get your Application, if the var is set to show the dialog, from your Activity code (meaning the UI Thread) show your dialog. You'd probably put this in your onResume code.

    In onPause, set the variable to show it next time. In onResume, set it as false.

    0 讨论(0)
提交回复
热议问题