Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

How to lock the screen of an android device

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

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


}

In Manifest add

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

Does anyone know what I am doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

This permission is needed:

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

UPDATE:

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

This and this might help you further.

UPDATE 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/topics/admin/device-admin.html#lock


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...