Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Password protect launch of android application

Tags:

android

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 shown.

I've tried some approaches to this (extending application class and so on) but none seems to work. Either they don't run on the UI thread or the dialog isn't shown on every launch/resume occasion.

// m

like image 219
m__ Avatar asked Aug 09 '10 07:08

m__


1 Answers

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
    }
}
like image 86
m__ Avatar answered Sep 29 '22 13:09

m__