【发布时间】:2011-07-20 13:22:55
【问题描述】:
我想在我的应用程序中实现屏幕锁定功能,但目前我无法让它工作。我有一个 alertDialog 将通过使用几个按钮请求用户输入。如果用户按“否”,我想锁定屏幕(无限期,而不是固定的时间)。以编程方式锁定屏幕的最佳方法是什么?我尝试使用以下方法,但尽管我的对话框被关闭,但屏幕永远不会锁定。
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.reenableKeyguard();
我的代码
import android.app.Activity;
import android.app.AlertDialog;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Window;
public class MyApp extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
startDialog();
}
private void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.setMessage("Do you want to exit the application?");
myAlertDialog.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
System.out.println("...yes button is clicked..");
arg0.dismiss();
}
});
myAlertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
System.out.println("...clicked no...");
arg0.dismiss();
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.reenableKeyguard();
}
});
AlertDialog alert = myAlertDialog.create();
myAlertDialog.setCancelable(false);
alert.setCancelable(false);
alert.getWindow().setLayout(600, 400);
myAlertDialog.show();
}
}
在清单中添加
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"></uses-permission>
有谁知道我做错了什么?
【问题讨论】:
-
@Matt Ball。我已经把我的整个源代码。你可以检查一下。我尝试过这种方法,但没有成功。
标签: android