开发者

reenablekeyguard not working

I am very new to Android and this is my first program. I have been trying to create a simple activity with a button which will lock the screen. Below is my code, but this is not working even after full executon when I come back to the front screen the screen is not locked. I maybe doing something stupis, but please help. Below is my code:

package com.droid.ScreenLock;

import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private KeyguardManager n;
    private Boolean b;
    private KeyguardLock l;

    /** Called when the activity is first created.开发者_如何学编程 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button tbut = (Button) findViewById(R.id.button1);
        tbut.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                n = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
                b = n.inKeyguardRestrictedInputMode();
            }
        });
    }

    public void onPause() {
        super.onPause();
        if (b != true) {
            l = (KeyguardLock) n.newKeyguardLock("User");
            l.reenableKeyguard();
            l = null;
        }
    }
}


The documentation for reenableKeyguard() states:

The keyguard will reappear if the previous call to disableKeyguard() caused it it to be hidden.

You did not disable the keyguard. Hence, you cannot re-enable the keyguard.


I don't have this problem as long as I don't reinitialize the keyguard. You should only keep one instance of keyguard (so make it a field variable). Then you know you're always renabling that one keyguard.

So....

  if (mKeyguardLock == null) {
        KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        mKeyguardLock = km.newKeyguardLock("MyKeyguardLock");
    }

    try{
        mKeyguardLock.reenableKeyguard(); //reenable before disabling for safety
    }
    catch(Exception e){
        //probably already reenabled
        Log.e(TAG, e.getMessage());
    }
    mKeyguardLock.disableKeyguard();


Killing the application that calls disableKeyguard() can cause it to reenable the keyguard. It worked for me!!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜