Android Studio Java开发:如何实现签到应用的导航锁定功能(禁止退出/返回需密码验证)
Hey there! Let's walk through how to build this navigation lock feature for your kids' check-in app step by step. I'll keep things simple since you're working with basic Java knowledge in Android Studio.
1. 定义全局状态变量
First, add a global boolean variable in your Anmeldung Activity to track whether navigation is locked, plus a fixed unlock password (you can change this later if needed):
public class Anmeldung extends AppCompatActivity { // Tracks if navigation is locked private boolean isNavigationLocked = false; // Customize your unlock password here private static final String UNLOCK_PASSWORD = "1234"; // ... Your existing onCreate() and other code }
2. 完善锁定按钮的AlertDialog逻辑
Update the PositiveButton click event to enable the locked state and notify the user:
eGeoeffnet.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder myAlertBuilder2 = new AlertDialog.Builder(Anmeldung.this); myAlertBuilder2.setTitle("Navigation sperren"); myAlertBuilder2.setMessage("Möchtest du die Navigation sperren?"); myAlertBuilder2.setPositiveButton("Ja", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Enable navigation lock isNavigationLocked = true; Toast.makeText(Anmeldung.this, "Navigation wurde gesperrt!", Toast.LENGTH_SHORT).show(); } }); myAlertBuilder2.setNegativeButton("Nein", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(Anmeldung.this, "Du hast 'Nein' ausgewählt!", Toast.LENGTH_SHORT).show(); } }); myAlertBuilder2.show(); } });
3. 拦截返回键并添加密码验证
Override the onBackPressed() method to trigger a password dialog when locked. Create a helper method for the unlock dialog:
@Override public void onBackPressed() { if (isNavigationLocked) { // Show password dialog when locked showUnlockDialog(); } else { // Allow normal back navigation if unlocked super.onBackPressed(); } } // Helper method for the unlock dialog private void showUnlockDialog() { // Create a password input field final EditText passwordInput = new EditText(this); passwordInput.setHint("Gib das Entsperrpasswort ein"); passwordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); AlertDialog.Builder unlockDialog = new AlertDialog.Builder(this); unlockDialog.setTitle("Navigation entsperren"); unlockDialog.setView(passwordInput); unlockDialog.setPositiveButton("Bestätigen", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String inputPassword = passwordInput.getText().toString().trim(); if (inputPassword.equals(UNLOCK_PASSWORD)) { // Unlock navigation if password is correct isNavigationLocked = false; Toast.makeText(Anmeldung.this, "Navigation wurde entsperrt!", Toast.LENGTH_SHORT).show(); // Allow back navigation after unlocking Anmeldung.super.onBackPressed(); } else { // Show error for wrong password Toast.makeText(Anmeldung.this, "Falsches Passwort!", Toast.LENGTH_SHORT).show(); } } }); unlockDialog.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Cancel and keep navigation locked dialog.dismiss(); } }); unlockDialog.setCancelable(false); // Prevent closing by tapping outside unlockDialog.show(); }
4. 处理Home键限制(重要说明)
Note: Android blocks regular apps from directly intercepting the Home key (this is a system security measure to stop malicious apps from hijacking the user's screen). But we can minimize accidental exits by pulling the app back to the foreground when it's sent to the background:
@Override protected void onPause() { super.onPause(); if (isNavigationLocked) { // Bring the app back to front if locked Intent intent = new Intent(this, Anmeldung.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } }
This won't fully block the Home key, but it will quickly return the app to the screen if a kid presses it. Combined with the password requirement for exiting, this should keep the app secure for your use case.
5. Extra: Ensuring Secure Exit
To make sure users can't exit via the recent apps menu without unlocking, the above onPause logic will pull the app back immediately. For full security, you can also add a check in onDestroy(), but the onPause method covers most accidental cases.
内容的提问来源于stack exchange,提问作者Veronique

