Search code examples
androidandroid-activitylockscreenondestroy

Android - My Activity breaks lockscreen


So I have an activity that goes over my lockscreen. But when I exit it it breaks my lockscreen. Ok, not literally... but I cant successfully LOCK my phone again. I have to restart it. I feel like I am just forgetting to put something in my onDestroy - like I have to reput the lockscreen after its done. Idk. What is the problem?

Lockscreen Activity:

@Override
public void onCreate(Bundle icicle){
    super.onCreate(icicle);
    setContentView(R.layout.lockscreen);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);//gotta get it to open on lock screen

    ImageView iv = (ImageView)findViewById(R.id.Background);
    //iv.getBackground().setAlpha(50);

    Button close = (Button)findViewById(R.id.Close);
    close.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
    final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("IN");
    kl.disableKeyguard();

    PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
    PowerManager.WakeLock wl=pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "My_App");
    wl.acquire();

    /*Builder adb = new AlertDialog.Builder(this);
    adb.setMessage("Testing");
    adb.setCancelable(false);
    adb.setNeutralButton("Close",new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which) {
            kl.reenableKeyguard();
        }
    });
    AlertDialog ad = adb.create();
    ad.show();*/

    wl.release();
}

As you see I just finish the activity. Am I forgetting to do something onDestroy?

Thanks


Solution

  • Wow... its as easy as this:

    kl.reenableKeyguard();
    

    put this is the button that cancels it:

    @Override
            public void onClick(View v) {
                kl.reenableKeyguard();
                finish();
            }
    

    Still trying to figure out how to make it uncancelable through the back button though....

    EDIT:

    Seems I dont need the final on KL. so this is how you would do it:

    KeyguardManager.KeyguardLock kl;
    
    @Override
    public void onDestroy(){
        kl.reenableKeyguard();
        super.onDestroy();
    }
    
    @Override
    public void onCreate(Bundle icicle){
        super.onCreate(icicle);
        setContentView(R.layout.lockscreen);
        KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
        kl = km.newKeyguardLock("IN");
        kl.disableKeyguard();
    }
    

    Just for anyone who needs this in the future.