【问题标题】:How to lock the screen of an android device如何锁定安卓设备的屏幕
【发布时间】: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>

有谁知道我做错了什么?

【问题讨论】:

标签: android


【解决方案1】:

有两种锁屏方式:

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" />

更新:

screenbrightness 有一个介于 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

【讨论】:

  • 我尝试了第二种方法,但第一种方法没有成功我不知道我必须设置什么权限。
  • 我使用了更新的代码,但问题是每次屏幕锁定,当我解锁屏幕时,它又被锁定了。
  • 用户询问的是如何锁定屏幕,而不是如何降低屏幕亮度。选项 1 不起作用,因为它需要权限 DEVICE_POWER。选择 2 什么都不做。
  • @A.Abiri,这对我也不起作用。你能看看这个吗?
【解决方案2】:

尝试像这样覆盖它:

 @Override
 protected void onResume() {
    // TODO Auto-generated method stub
    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.screenBrightness = 1;
    getWindow().setAttributes(params);
    super.onResume();
}

它确实对我有帮助:D

【讨论】:

    【解决方案3】:

    我花了一整天的时间来决定是将屏幕亮度更改为 0 还是仅触发锁定本身。

    Here is an entire example on github for those of you who would like to see all of the required code in action! Don't forget to check out the lock.xml file! 这就是所有使用策略的所在!看起来有点像!

    <?xml version="1.0" encoding="UTF-8"?>
    <device-admin xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <uses-policies>
    
            <force-lock />
        </uses-policies>
    
    </device-admin>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多