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