8

I want to implement the screen lock functionality in my application, but currently I can't get it to work. I have an alertDialog which will request input from the user through the use of a couple buttons. If the user presses 'no' I want to lock the screen (indefinitely, not for a set amount of time). What is the best way to programmatically lock the screen? I have tried using the following but, although my dialog is dismissed, the screen never locks.

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
                        KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
                        lock.reenableKeyguard();

MyCode

import android.app.Activity;
import android.app.AlertDialog;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Window;

public class MyApp extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);


        startDialog();
    }



    private void startDialog() {

        AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);

        myAlertDialog.setMessage("Do you want to exit the application?");
        myAlertDialog.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {

                    // do something when the button is clicked
                    public void onClick(DialogInterface arg0, int arg1) {
                        System.out.println("...yes button is clicked..");
                        arg0.dismiss();

                    }
                });

        myAlertDialog.setNegativeButton("NO",
                new DialogInterface.OnClickListener() {

                    // do something when the button is clicked
                    public void onClick(DialogInterface arg0, int arg1) {
                        System.out.println("...clicked no...");
                        arg0.dismiss();
                        KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
                        KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
                        lock.reenableKeyguard();

                    }
                });
        AlertDialog alert = myAlertDialog.create();
        myAlertDialog.setCancelable(false);
        alert.setCancelable(false);
        alert.getWindow().setLayout(600, 400);

        myAlertDialog.show();
    }


}

In Manifest add

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"></uses-permission>

Does anyone know what I am doing wrong?

matt5784
  • 3,065
  • 2
  • 24
  • 42
  • possible duplicate of [how to programmaticaly lock screen android](http://stackoverflow.com/questions/3594532/how-to-programmaticaly-lock-screen-android) – Matt Ball Jul 20 '11 at 13:24
  • @Matt Ball. I have put my whole source code. you can check. I have tried that approach but didnot get success. –  Jul 20 '11 at 13:40

3 Answers3

7

There are two way you can lock the screen:

PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);

// Choice 1
manager.goToSleep(int amountOfTime);

// Choice 2
PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Your Tag");
wl.acquire();
wl.release();

This permission is needed:

<uses-permission android:name="android.permission.WAKE_LOCK" />

UPDATE:

The screenbrightness has a value between 0 and 1. If the value is set to a negative number, it will be set to the default screen brightness. By changing the brightness to 0, the brightness will be low enough that the screen will automatically turn off.

WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 0;
getWindow().setAttributes(params);

This and this might help you further.

UPDATE 2:

Here are some links to methods that have been used for locking the screen:

http://rdcworld-android.blogspot.in/2012/03/lock-phone-screen-programmtically.html

http://developer.android.com/guide/topics/admin/device-admin.html#lock

A. Abiri
  • 10,750
  • 4
  • 30
  • 31
  • I tried the second approach but didnot get success for first approach i donot know what is the permission that i have to set. –  Jul 22 '11 at 10:55
  • I used the Updated code, but the problem is that every time screen locks, and as i unlock the screen, then it is again locked. – DEVANG SHARMA Oct 07 '11 at 10:39
  • 1
    the user asked for how to lock the screen, not how to lower the screen bightness. Choice 1 does not work because it needs permission DEVICE_POWER. Choice 2 is doing nothing. – MilMike Oct 05 '13 at 12:11
  • @A.Abiri, It's not working for me too. can you please look at this once. – Bipin Vayalu Dec 23 '13 at 16:34
3

Try to override it like this:

 @Override
 protected void onResume() {
    // TODO Auto-generated method stub
    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.screenBrightness = 1;
    getWindow().setAttributes(params);
    super.onResume();
}

It does help me :D

eeerahul
  • 1,629
  • 4
  • 27
  • 38
1

I just spent an entire day deciding whether to just change the screen brightness to 0 or just proc the lock itself.

Here is an entire example on github for those of you who would like to see all of the required code in action! Don't forget to check out the lock.xml file! That is where all of the uses-policies goes! looks a bit like this exactly!

<?xml version="1.0" encoding="UTF-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <uses-policies>

        <force-lock />
    </uses-policies>

</device-admin>
Urasquirrel
  • 1,461
  • 1
  • 18
  • 34