要为你的Android应用设置密码或PIN码,你可以使用Android提供的KeyStore和KeyguardManager来实现。以下是一个示例代码,演示了如何实现这一功能:
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.util.Log;
import androidx.annotation.RequiresApi;
import java.security.KeyStore;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class MainActivity extends Activity {
private static final String KEY_NAME = "my_key";
private static final int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
if (keyguardManager.isKeyguardSecure()) {
createKey();
Cipher cipher = getCipher();
if (cipher != null) {
Intent intent = keyguardManager.createConfirmDeviceCredentialIntent("Unlock", "Confirm your screen lock PIN or password");
if (intent != null) {
startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
} else {
// Device does not support screen lock
}
} else {
// Cipher initialization failed
}
} else {
// Device does not have a screen lock
}
} else {
// Device is running on a version prior to Marshmallow
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void createKey() {
try {
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
keyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setUserAuthenticationRequired(true)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
.build());
keyGenerator.generateKey();
} catch (Exception e) {
Log.e("TAG", Log.getStackTraceString(e));
}
}
private Cipher getCipher() {
try {
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null);
Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/"
+ KeyProperties.ENCRYPTION_PADDING_PKCS7);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher;
} catch (Exception e) {
Log.e("TAG", Log.getStackTraceString(e));
}
return null;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
if (resultCode == RESULT_OK) {
// User has successfully authenticated, perform your app operations here
} else {
// User failed to authenticate, handle accordingly
}
}
}
}
这段代码将在应用启动时检查设备是否有屏幕锁定,并要求用户输入密码或PIN码进行验证。如果验证成功,你可以在onActivityResult方法中执行你的应用逻辑。
请注意,上述代码仅适用于Android 6.0(Marshmallow)及更高版本。在更早的版本中,你可以使用不同的方法来实现此功能。