Raluca Lucaci
Raluca Lucaci

Reputation: 2153

Lock screen just for my app

I would like to have a lock screen just to secure my app . For example if user let s the app in background and enters again lock screen should appear . Same result if the user is in an activity and he does not do any action for x time .

I used broadcast receiver but it appears all time - if i am not in my app passcode appears .

        <receiver
        android:name=".event.broadcast.LockBroadcastRecever"
        android:enabled="true"
        android:exported="false" >
        <intent-filter android:priority="1" >
            <action android:name="android.intent.action.SCREEN_ON" />
            <action android:name="android.intent.action.SCREEN_OFF" />
            <action android:name="android.intent.action.USER_PRESENT" />

        </intent-filter>
    </receiver>

Is there a method to constraint this BR to handle just my app

Upvotes: 0

Views: 72

Answers (1)

Napolean
Napolean

Reputation: 5543

Few days back I implemented a similar feature in my app and hence also created a tip for that which is as follows :

In this tip , we will talk about the minimum code required to secure unauthorised access to the app once screen has timed out.

Scenario : An app with a pin/password enabled login system. So what if app is left open by mistake and after a small time screen timesout assuming not in user's access anymore. Now what could be the simplest approach to prevent unauthorized access to the app ?

There are two approach to do these whenver screen times out: 1. Implement a pin/password enabled module that provides entry-point to the rest of the modules. 2. Reset the application.

You can define the following two methods in your Utility class.

// Checks if screen has timed out

private static boolean isScreenLocked() {
    return !((PowerManager) MainApplication.getsApplicationContext().getSystemService(Context.POWER_SERVICE)).isScreenOn();
}

// Checks screenout and defines action to do when screen timed out

public static void launchPinActivityWithTimeout(FragmentActivity iActivity) {

    if (isScreenLocked()) {

        //TODO Below code should be the action on screen timed out event. For instance in following code I am launching an activity that contains pin entry form. One could also finish all the activities and restart the app. Similarly many more actions

            Intent logoutIntent = new Intent(iActivity, PinActivationActivity.class);

    //TODO Send any specific information indicating this activity is launched as a result of screen timeout. Here sending boolean indicating device back button must be disabled
            logoutIntent.putExtra("back_should_not_work", true);

    iActivity.startActivity(logoutIntent);
    }
}

I called the above method launchPinActivityWithTimeout() in onPause() method of each activity as onPause will always be called for each activity whenever screen timedout.

Please check it if it helps you.

Upvotes: 1

Related Questions