- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想使用 Pixel 2 背面的指纹传感器在点击应用程序中的按钮时按下该按钮。
我在文档中找到了一些相关内容,但看起来它只报告滑动输入而不报告点击输入:https://developer.android.com/reference/android/accessibilityservice/FingerprintGestureController.html
最佳答案
是的,这是真的。迈克尔·多德正在讲述什么。我们可以获得各种事件,例如指纹成功、失败或快速删除等事件。点击事件更有可能是指纹被快速移除。我没有找到任何直接获取点击事件的 api。相反,请使用身份验证来获取自定义 Activity 中的点击。实现以下代码并找到用于处理点击事件的各种适当的事件。
@TargetApi(Build.VERSION_CODES.M)
public class FingerprintUiHelper extends FingerprintManager.AuthenticationCallback {
/**
* The timeout for the error to be displayed. Returns to the normal UI after this.
*/
private static final long ERROR_TIMEOUT_MILLIS = 1600;
/**
* The timeout for the success to be displayed. Calls {@link Callback#onAuthenticated()} after this.
*/
private static final long SUCCESS_DELAY_MILLIS = 1300;
/**
* Alias for our key in the Android Key Store
**/
private static final String KEY_NAME = "my_key";
/**
* The {@link Cipher} used to init {@link FingerprintManager}
*/
private Cipher mCipher;
/**
* The {@link KeyStore} used to initiliaze the key {@link #KEY_NAME}
*/
private KeyStore mKeyStore;
/**
* The {@link KeyGenerator} used to generate the key {@link #KEY_NAME}
*/
private KeyGenerator mKeyGenerator;
/**
* The {@link android.hardware.fingerprint.FingerprintManager.CryptoObject}
*/
private final FingerprintManager mFingerprintManager;
/**
* The {@link ImageView} that is used to show the authent state
*/
private final ImageView mIcon;
/**
* The {@link TextView} that is used to show the authent state
*/
private final TextView mErrorTextView;
private final Callback mCallback;
/**
* The {@link CancellationSignal} used after an error happens
*/
private CancellationSignal mCancellationSignal;
/**
* Used if the user cancelled the authentication by himself
*/
private boolean mSelfCancelled;
/**
* Builder class for {@link FingerprintUiHelper} in which injected fields from Dagger
* holds its fields and takes other arguments in the {@link #build} method.
*/
public static class FingerprintUiHelperBuilder {
private final FingerprintManager mFingerPrintManager;
public FingerprintUiHelperBuilder(FingerprintManager fingerprintManager) {
mFingerPrintManager = fingerprintManager;
}
public FingerprintUiHelper build(ImageView icon, TextView errorTextView, Callback callback) {
return new FingerprintUiHelper(mFingerPrintManager, icon, errorTextView,
callback);
}
}
/**
* Constructor for {@link FingerprintUiHelper}. This method is expected to be called from
* only the {@link FingerprintUiHelperBuilder} class.
*/
private FingerprintUiHelper(FingerprintManager fingerprintManager,
ImageView icon, TextView errorTextView, Callback callback) {
mFingerprintManager = fingerprintManager;
mIcon = icon;
mErrorTextView = errorTextView;
mCallback = callback;
}
/**
* Starts listening to {@link FingerprintManager}
*
* @throws SecurityException If the hardware is not available, or the permission are not set
*/
public void startListening() throws SecurityException {
if (initCipher()) {
FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(mCipher);
if (!isFingerprintAuthAvailable()) {
return;
}
mCancellationSignal = new CancellationSignal();
mSelfCancelled = false;
mFingerprintManager.authenticate(cryptoObject, mCancellationSignal, 0 /* flags */, this, null);
mIcon.setImageResource(R.drawable.ic_fp_40px);
}
}
/**
* Stops listening to {@link FingerprintManager}
*/
public void stopListening() {
if (mCancellationSignal != null) {
mSelfCancelled = true;
mCancellationSignal.cancel();
mCancellationSignal = null;
}
}
/**
* Called by {@link FingerprintManager} if the authentication threw an error.
*/
@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
if (!mSelfCancelled) {
showError(errString);
mIcon.postDelayed(new Runnable() {
@Override
public void run() {
mCallback.onError();
}
}, ERROR_TIMEOUT_MILLIS);
}
}
/**
* Called by {@link FingerprintManager} if the user asked for help.
*/
@Override
public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
showError(helpString);
}
/**
* Called by {@link FingerprintManager} if the authentication failed (bad finger etc...).
*/
@Override
public void onAuthenticationFailed() {
showError(mIcon.getResources().getString(
R.string.pin_code_fingerprint_not_recognized));
}
/**
* Called by {@link FingerprintManager} if the authentication succeeded.
*/
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
mErrorTextView.removeCallbacks(mResetErrorTextRunnable);
mIcon.setImageResource(R.drawable.ic_fingerprint_success);
mErrorTextView.setTextColor(
mErrorTextView.getResources().getColor(R.color.success_color, null));
mErrorTextView.setText(
mErrorTextView.getResources().getString(R.string.pin_code_fingerprint_success));
mIcon.postDelayed(new Runnable() {
@Override
public void run() {
mCallback.onAuthenticated();
}
}, SUCCESS_DELAY_MILLIS);
}
/**
* Tells if the {@link FingerprintManager#isHardwareDetected()}, {@link FingerprintManager#hasEnrolledFingerprints()},
* and {@link KeyguardManager#isDeviceSecure()}
*
* @return true if yes, false otherwise
* @throws SecurityException If the hardware is not available, or the permission are not set
*/
public boolean isFingerprintAuthAvailable() throws SecurityException {
return mFingerprintManager.isHardwareDetected()
&& mFingerprintManager.hasEnrolledFingerprints()
&& ((KeyguardManager) mIcon.getContext().getSystemService(Context.KEYGUARD_SERVICE)).isDeviceSecure();
}
/**
* Initialize the {@link Cipher} instance with the created key in the {@link #createKey()}
* method.
*
* @return {@code true} if initialization is successful, {@code false} if the lock screen has
* been disabled or reset after the key was generated, or if a fingerprint got enrolled after
* the key was generated.
*/
private boolean initCipher() {
try {
if (mKeyStore == null) {
mKeyStore = KeyStore.getInstance("AndroidKeyStore");
}
createKey();
mKeyStore.load(null);
SecretKey key = (SecretKey) mKeyStore.getKey(KEY_NAME, null);
mCipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
mCipher.init(Cipher.ENCRYPT_MODE, key);
return true;
} catch (NoSuchPaddingException | KeyStoreException | CertificateException | UnrecoverableKeyException | IOException
| NoSuchAlgorithmException | InvalidKeyException e) {
return false;
}
}
/**
* Creates a symmetric key in the Android Key Store which can only be used after the user has
* authenticated with fingerprint.
*/
public void createKey() {
// The enrolling flow for fingerprint. This is where you ask the user to set up fingerprint
// for your flow. Use of keys is necessary if you need to know if the set of
// enrolled fingerprints has changed.
try {
// Set the alias of the entry in Android KeyStore where the key will appear
// and the constrains (purposes) in the constructor of the Builder
mKeyGenerator = KeyGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
mKeyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME,
KeyProperties.PURPOSE_ENCRYPT |
KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
// Require the user to authenticate with a fingerprint to authorize every use
// of the key
.setUserAuthenticationRequired(true)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
.build());
mKeyGenerator.generateKey();
} catch (NoSuchProviderException | NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
throw new RuntimeException(e);
}
}
/**
* Show an error on the UI using {@link #mIcon} and {@link #mErrorTextView}
*/
private void showError(CharSequence error) {
mIcon.setImageResource(R.drawable.ic_fingerprint_error);
mErrorTextView.setText(error);
mErrorTextView.setTextColor(
mErrorTextView.getResources().getColor(R.color.warning_color, null));
mErrorTextView.removeCallbacks(mResetErrorTextRunnable);
mErrorTextView.postDelayed(mResetErrorTextRunnable, ERROR_TIMEOUT_MILLIS);
}
/**
* Run by {@link #showError(CharSequence)} with delay to reset the original UI after an error.
*/
Runnable mResetErrorTextRunnable = new Runnable() {
@Override
public void run() {
mErrorTextView.setTextColor(
mErrorTextView.getResources().getColor(R.color.hint_color, null));
mErrorTextView.setText(
mErrorTextView.getResources().getString(R.string.pin_code_fingerprint_text));
mIcon.setImageResource(R.drawable.ic_fp_40px);
}
};
/**
* The interface used to call the original Activity/Fragment... that uses this helper.
*/
public interface Callback {
void onAuthenticated();
void onError();
}
关于java - 在应用程序中检测指纹传感器点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51596736/
我想通过自己编写的C API来控制一些物理引擎,开关,灯光。 我已经对如何实现这一目标有了一个非常全面的概述: 使用内核抽象,在其上编写驱动程序并将其用作控制。驱动程序本身必须管理传入字节并解释它们(
过去做过一些 Android 开发,但已经有一段时间了 - 想构建一个使用方向和接近传感器的应用程序。所以... mSensorManager = (SensorManager) getSystemS
Android SDK 实际上提供了一个很好的接口(interface)来访问传感器。但是例如线性加速度传感器可以评估为 documentation从重力和加速度描述 - 所以这个传感器没有真正的物理
我是 android 开发的新手,我需要查看 android 3.1 设备上的传感器列表。 我有这个代码: package milos.exemplesensorlist; import java.u
我正在尝试为网络传感器实现虫洞攻击检测程序。我在互联网上四处寻找这方面的源代码,但一无所获。有谁知道在哪里可以找到虫洞传感器攻击(用于模拟目的)或虫洞检测的源代码?如果是这样,在哪里?它可以是任何编程
有没有办法使用内置传感器从 Android 手机获取二氧化碳。目前我通过环境传感器获得的是当前速度、气压、湿度和噪音幅度。 如果这是不可能的,有没有一种方法可以计算二氧化碳(该值可以是估计值)? 最佳
我正在尝试访问我的 Samsung Galaxy S3 上的传感器(特别是接近传感器和 RGBW 传感器),并且已获得可运行的代码来访问接近传感器。但是,我只得到值 1 或 8(近/远)。我知道有时硬
我想知道是否可以通过javascript获取诺基亚5800的传感器值。我知道在 HTML5 中有一个事件 DeviceOrientationEvent 可以提供一些值。但由于诺基亚 5800 没有 H
我想知道如何正确使用“旋转 vector 传感器”的输出。目前我想到了以下内容,并想根据 result[] 计算偏航和俯仰,以便了解设备指向的位置(处于横向模式)。但我对结果有疑问。偏航计算非常精确,
我正在编写一个方法,如果存在并启用了 GPS 传感器,该方法将返回 true,但如果不存在或已关闭,则返回 false。事实证明这很难,因为... hasSystemFeature("FEATURE_
我正在尝试从 Samsung Galaxy Tab GT-P1000 读取多个传感器,相对于我使用的应用程序,它们似乎非常占用 CPU。 作为测试,我创建了一个简短的程序,它为加速度计传感器实现了 S
我正在像这样从重新启动的接收步骤创建一个演示。 public class MainActivity extends AppCompatActivity implements SensorEventLi
我正在开发用于汽车加速跟踪的应用程序。我使用标准加速度计,事先在特定位置进行校准。 然后,假设手机的方向没有改变,我记录了指定时间的加速度计数据并计算了移动参数,其中之一是测试结束时汽车的速度。 在笔
下面是API中定义的几个代表sensor的常量。 Int TYPE_ACCELEROMETER A constant desc
我使用的 Arduino 具有多个 (3) 个传感器,使用正常模式(非寄生模式)连接到数字引脚 2。 其中两个传感器是使用库“OneWire”(Library Page)和“DallasTempera
我想让 android 传感器与 opengl 一起工作,以将 opengl 的相机旋转到手机指向的任何地方。 详细说明:如果玩家正在看东方,我希望 opengl 的相机在游戏中也指向东方;如果玩家指
我正在使用 Google Cardboard(它的 HeadTracker 类)来检测 AR 应用程序中有关设备旋转的某些事情。效果很好。 但是,在某些设备上,它不起作用(没有任何反应)。我认为这是因
是否有适用于 Apple Watch Kit 传感器(例如加速度计、心率监测器、触觉传感器)的 API? 我如何访问这些传感器? 最佳答案 WatchOS 2.0 的 Watchkit 中现已提供传感
我正在学习如何使用 Pyfst 创建换能器,并且我正在尝试可视化我创建的换能器。最终目标是能够将传感器写入点文件并在 Graphviz 中查看它们。 我拿了一个示例代码来看看如何可视化下面的接受器。
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我是一名优秀的程序员,十分优秀!