gpt4 book ai didi

android - 如何锁定安卓设备的屏幕

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:20:40 25 4
gpt4 key购买 nike

我想在我的应用程序中实现屏幕锁定功能,但目前无法实现。我有一个 alertDialog,它将通过使用几个按钮请求用户输入。如果用户按下“否​​”,我想锁定屏幕(无限期地,而不是固定的时间)。以编程方式锁定屏幕的最佳方法是什么?我尝试使用以下方法,但尽管我的对话框被关闭,但屏幕永远不会锁定。

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.reenableKeyguard();

MyCode

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();
}


}

在 list 中添加

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"></uses-permission>

有人知道我做错了什么吗?

最佳答案

有两种方法可以锁定屏幕:

PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);

// Choice 1
manager.goToSleep(int amountOfTime);

// Choice 2
PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Your Tag");
wl.acquire();
wl.release();

需要此权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />

更新:

屏幕亮度的值介于 0 和 1 之间。如果该值设置为负数,则会设置为默认屏幕亮度。通过将亮度更改为 0,亮度将足够低,屏幕将自动关闭。

WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 0;
getWindow().setAttributes(params);

Thisthis可能会进一步帮助您。

更新 2:

以下是一些用于锁定屏幕的方法的链接:

http://rdcworld-android.blogspot.in/2012/03/lock-phone-screen-programmtically.html

http://developer.android.com/guide/topics/admin/device-admin.html#lock

关于android - 如何锁定安卓设备的屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6762671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com