您的位置:首页 > 移动开发 > Android开发

Android 锁定屏幕 不可解锁

2017-04-25 17:09 477 查看

Android锁定屏幕

这个玩意只是在MDM上有所应用,通过后台发送指令到Android上,或者是Android程序本身检测满足某一条件的时候,对Android手机进行锁屏操作,当前这种方法,只能通过硬件重启的方式解除锁屏,即音量键+锁屏键

好了,闲言少叙,这个操作主要涉及到下面这个类

LockScreenUtil 主要类

package com.example.demo;

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;

public class LockScreenUtil {

Point mLpSize;
Button mBtnUnlock;
ViewGroup mView;
final WindowManager mWindowManager;
final WindowManager.LayoutParams mLp;

public LockScreenUtil(Context mContext){

mView = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.lock_screen_view, null);
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
mLp = new WindowManager.LayoutParams();
mBtnUnlock = (Button)mView.findViewById(R.id.btn_unlock);

mBtnUnlock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show(false);
}
});

initLp();

}

private void initLp(){
mLp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;// TYPE_SYSTEM_ALERT;
mLp.format = PixelFormat.RGBA_8888;

mLp.flags |= WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

mLp.systemUiVisibility = View.STATUS_BAR_HIDDEN;

mLp.width = WindowManager.LayoutParams.MATCH_PARENT;
mLp.height = WindowManager.LayoutParams.MATCH_PARENT;

}

public void show(boolean flag){
if (flag){
mWindowManager.addView(mView, mLp);
} else {
mWindowManager.removeView(mView);
}
}
}


Activity中涉及到的东西就相对较少了

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mBtnLock = (Button)findViewById(R.id.btn);
mLockScreen = new LockScreen(this);

mBtnLock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mLockScreen.show(true);
}
});

}


lock_screen_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

</LinearLayout>


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="LOck" >
</Button>
</LinearLayout>


最后不要忘记权限

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


运行之后点击Lock 即可锁定屏幕,当然程序还不完美,还需继续修改完善,有时间继续更

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 锁定屏幕