我对Android非常陌生,这是我的第一个程序。我一直在尝试创建一个简单的活动与按钮,这将锁定屏幕。下面是我的代码,但这是不工作,即使在完全执行后,当我回到前面的屏幕屏幕没有锁定。我可能做了件愚蠢的事,但请帮帮忙。下面是我的代码:
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;
}
}
}发布于 2011-04-25 06:10:19
reenableKeyguard()的文档说明:
如果之前对disableKeyguard()的调用导致键盘守卫被隐藏,则
键盘守卫将重新出现。
您没有禁用键盘保护功能。因此,您不能重新启用键盘守卫。
发布于 2014-07-13 01:34:48
只要不重新初始化键盘守卫,我就不会有这个问题。您应该只保留keyguard的一个实例(因此将其设置为字段变量)。然后你就会知道你总是在重新启用那个键盘守卫。
所以..。
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();发布于 2012-07-21 12:12:50
终止调用disableKeyguard()的应用程序可能会导致它重新启用键盘守卫。它对我起作用了!
https://stackoverflow.com/questions/5773504
复制相似问题