最近我在模拟器锁定和解锁屏幕上做了一个项目。我按了一个按钮。如果我按下那个按钮我想要锁定手机,我想这样做。
我的问题是当我按下按钮时没有发生任何事情。 请给我任何想法。
如何使用此代码执行锁定解锁。
public class Test extends Activity implements OnClickListener{
/** Called when the activity is first created. */
Button btn1;
private KeyguardManager mKeyguardManager;
private KeyguardManager.KeyguardLock mKeyguardLock;
private static final String TAG = "ALERTLock";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1=(Button)findViewById(R.id.button1);
btn1.setOnClickListener(this);
int flags = getFlagsForVersion();
getWindow().addFlags(flags);
mKeyguardManager = (KeyguardManager)
getSystemService(Context.KEYGUARD_SERVICE);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==btn1)
{
disableKeyguard();
}
else
{
enableKeyguard();
}
}
private int getFlagsForVersion() {
final String possibleFlags[] = new String[] {
"FLAG_SHOW_WHEN_LOCKED",
"FLAG_DISMISS_KEYGUARD",
"FLAG_TURN_SCREEN_ON"
};
int flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
for(String flag:possibleFlags) {
try {
Field field = WindowManager.LayoutParams.class.getField(flag);
int value = field.getInt(null);
flags |= value;
}
catch(NoSuchFieldException e) { }
catch(IllegalAccessException e) { }
}
return flags;
}
private synchronized void enableKeyguard() {
if (mKeyguardLock != null) {
mKeyguardLock.reenableKeyguard();
mKeyguardLock = null;
}
}
private synchronized void disableKeyguard() {
if (mKeyguardLock == null) {
mKeyguardLock = mKeyguardManager.newKeyguardLock(TAG);
mKeyguardLock.disableKeyguard();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
disableKeyguard();
}
@Override
public void onResume() {
super.onResume();
disableKeyguard();
}
}
答案 0 :(得分:0)
//尝试一次 并检查logcat是否正在打印行启用
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
//do this
Log.d("clicked enable","-----");
enableKeyguard();
break;
}