1

I'm building a app which wakes and unlock's the phone on a event.

This works fine..

only when leaving the activity (or when a timer runs out..for when there's nobody around) i use reenableKeyguard() to restore the keylock to origenal state...

but then it locks directly....

i would like the normal behavour...so when the user exit's my app..there's no lock yet. but after 30 sec. of no activity phone goes to sleep and sets lock.

how can i do this... thanks.

Below is the code I have used for this:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    getWindow().addFlags(
            LayoutParams.FLAG_DISMISS_KEYGUARD
            | LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | LayoutParams.FLAG_TURN_SCREEN_ON
            | LayoutParams.FLAG_KEEP_SCREEN_ON
            );



    KeyguardManager manager = (KeyguardManager) getSystemService
    (Context.KEYGUARD_SERVICE);
    lock = manager.newKeyguardLock
    ("hh");

    lock.disableKeyguard();
  }


   protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    lock.reenableKeyguard();


}

PLease let me know what are the changes I need to do in above code so it works nicely.

ThomasW
  • 16,981
  • 4
  • 79
  • 106
brig
  • 3,721
  • 12
  • 43
  • 61

4 Answers4

1

this keygaurd method works for less than 2.0 if u r using >=2.2 then change the approach. go for power manager,

UdiT
  • 609
  • 1
  • 6
  • 21
1

As per the Device Administration API:

You can also programmatically tell the device to lock immediately:

DevicePolicyManager mDPM; mDPM.lockNow();

And as per the API:

Make the device lock immediately, as if the lock screen timeout has expired at the point of this call.

Vaiden
  • 15,728
  • 7
  • 61
  • 91
0

you can use thi code for locking device screen

 protected void onResume() {


     IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
        registerReceiver(mIntentReceiver, filter);
           }

 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver(){

           public void onReceive(Context context, Intent intent) {


            System.out.println("phone locked"); 

    }
Nikhil
  • 16,194
  • 20
  • 64
  • 81
0

check this and http://developer.android.com/guide/topics/admin/device-admin.html

Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
  • Vineet ,Thanks for your help , but this will only help in lock the screen after some predetermined time. But my app should lock the screen naturally after my onStop() getting called. – brig Oct 04 '11 at 13:41