如何锁定Android设备的屏幕 [英] How to lock the screen of an android device

查看:129
本文介绍了如何锁定Android设备的屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现我的应用程序的屏幕锁定功能,但目前我无法得到它的工作。我有一个alertDialog这将通过使用一对夫妇按钮请求来自用户的输入。如果用户presses'不',我想锁定屏幕(无限期地,没有一个设定的时间量)。什么是编程锁定屏幕的最佳方法是什么?我已经尝试使用下面的,但是,尽管被驳回我的对话框,在屏幕锁定从来没有尝试过。

I want to implement the screen lock functionality in my application, but currently I can't get it to work. I have an alertDialog which will request input from the user through the use of a couple buttons. If the user presses 'no' I want to lock the screen (indefinitely, not for a set amount of time). What is the best way to programmatically lock the screen? I have tried using the following but, although my dialog is dismissed, the screen never locks.

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

我的code

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>

有谁知道我在做什么错了?

Does anyone know what I am doing wrong?

推荐答案

有,你可以锁定屏幕双向的:

There are two way you can lock the screen:

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,亮度将是足够低,屏幕会自动关闭。

The screenbrightness has a value between 0 and 1. If the value is set to a negative number, it will be set to the default screen brightness. By changing the brightness to 0, the brightness will be low enough that the screen will automatically turn off.

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

和<一href="http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#BRIGHTNESS_OVERRIDE_FULL"相对=nofollow>这个可以帮助你进一步的。

This and this might help you further.

更新2:

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

Here are some links to methods that have been used for locking the screen:

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

http://developer.android.com/guide/主题/管理/设备admin.html#锁定

这篇关于如何锁定Android设备的屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆