简体   繁体   English

以编程方式锁定屏幕

[英]Lock screen Programmatically

I use this code to get pattern lock but it does not work. 我使用此代码来获取模式锁定,但是它不起作用。 I want to lock the screen on button click. 我想在单击按钮时锁定屏幕。

main.xml main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Lock"
        android:onClick="lock"/>

</RelativeLayout>

MainActivity.java MainActivity.java

public class MainActivity extends ActionBarActivity {

    DevicePolicyManager mDPM;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
    }

    public void lock(View view) {
        mDPM.lockNow();
    }
}

I want to do like this 我想这样 在此处输入图片说明

Have you tried this link.. 您是否尝试过链接。

lock/unlock screen 锁定/解锁屏幕

In case it doesn't work, try android version other than ICS if you are using ICS 如果无法正常使用,请尝试使用ICS以外的Android版本(如果您使用的是ICS)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM