package com.lidaochen.test03;
import android.app.ActivityManager;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText et_pwd;
private HomeReceiver innerReceiver;
// 下面的函数 是屏蔽掉返回键
@Override
public void onBackPressed) {
}
@Override
protected void onCreateBundle savedInstanceState) {
super.onCreatesavedInstanceState);
setContentViewR.layout.activity_main);
// 找到 EditText 控件
et_pwd = EditText)findViewByIdR.id.et_pwd);
// 动态注册广播接收者
innerReceiver = new HomeReceiver);
// 创建 IntentFilter 对象
IntentFilter intentFilter = new IntentFilterIntent.ACTION_CLOSE_SYSTEM_DIALOGS);
// 注册广播接收者
registerReceiverinnerReceiver, intentFilter);
}
// 这个函数主要是为了 屏蔽掉 recent APPS 按键
@Override
protected void onPause) {
super.onPause);
for int j = 0; j < 50; j++){
ActivityManager activityManager = ActivityManager) getApplicationContext).getSystemServicethis.ACTIVITY_SERVICE);
activityManager.moveTaskToFrontgetTaskId), 0);
}
}
// 密码验证按钮点击事件处理函数
public void clickView v)
{
// 获取用户输入的密码
String pwd = et_pwd.getText).toString).trim);
if pwd.equals"123456"))
{
Toast.makeTextgetApplicationContext), "密码正确!", Toast.LENGTH_SHORT).show);
System.exit0);
}
else
{
Toast.makeTextgetApplicationContext), "密码错误!", Toast.LENGTH_SHORT).show);
}
}
}
package com.lidaochen.test03;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class HomeReceiver extends BroadcastReceiver {
static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
static public final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
static public final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
static public final String SYSTEM_DIALOG_REASON_ASSIST = "assist";
@Override
public void onReceiveContext arg0, Intent arg1) {
String action = arg1.getAction);
// 按下Home键会发送 ACTION_CLOSE_SYSTEM_DIALOGS的广播
if action.equalsIntent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = arg1.getStringExtraSYSTEM_DIALOG_REASON_KEY);
if reason != null) {
if reason.equalsSYSTEM_DIALOG_REASON_HOME_KEY)) {
Intent intent = new Intentarg0, MainActivity.class);
// 点击home键无延时,且不会产生新的activity
intent.setFlagsIntent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivityarg0, 0, intent, 0);
try
{
pendingIntent.send);
}
catch PendingIntent.CanceledException e)
{
e.printStackTrace);
}
}
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解锁请加qq:1304168666"
android:textColor="#ffff0f00"/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="请输入密码..."
android:textColorHint="#ff9b05ff"
android:textColor="#ff0006ff"
android:layout_marginTop="30dp"
android:lines="1"
android:inputType="text"
android:id="@+id/et_pwd"/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="确定"
android:onClick="click"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="技术支持:李道臣"/>
</LinearLayout>
参考链接:https://blog.csdn.net/qq_37820491/article/details/80940582