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 onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到 EditText 控件
et_pwd = (EditText)findViewById(R.id.et_pwd);
// 动态注册广播接收者
innerReceiver = new HomeReceiver();
// 创建 IntentFilter 对象
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
// 注册广播接收者
registerReceiver(innerReceiver, intentFilter);
}
// 这个函数主要是为了 屏蔽掉 recent APPS 按键
@Override
protected void onPause() {
super.onPause();
for (int j = 0; j < 50; j++){
ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(this.ACTIVITY_SERVICE);
activityManager.moveTaskToFront(getTaskId(), 0);
}
}
// 密码验证按钮点击事件处理函数
public void click(View v)
{
// 获取用户输入的密码
String pwd = et_pwd.getText().toString().trim();
if (pwd.equals("123456"))
{
Toast.makeText(getApplicationContext(), "密码正确!", Toast.LENGTH_SHORT).show();
System.exit(0);
}
else
{
Toast.makeText(getApplicationContext(), "密码错误!", 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 onReceive(Context arg0, Intent arg1) {
String action = arg1.getAction();
// 按下Home键会发送 ACTION_CLOSE_SYSTEM_DIALOGS的广播
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = arg1.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if (reason != null) {
if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
Intent intent = new Intent(arg0, MainActivity.class);
// 点击home键无延时,且不会产生新的activity
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(arg0, 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