【问题标题】:Continuously listen for fingerprints on android device in background在后台持续监听安卓设备上的指纹
【发布时间】:2018-03-06 05:01:42
【问题描述】:

我正在开发一个指纹手势应用程序,该应用程序应该在后台运行并不断听取用户的指纹输入以执行所需的操作。到目前为止,我已经尝试使用 IntentService 让指纹扫描仪在后台工作,但是一旦我关闭活动或最小化它,指纹扫描仪就会停止工作。即使在我的活动关闭后,有什么方法可以在后台使用指纹扫描仪? 这是我的代码

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private static final String KEY_NAME = "secretkey";
    private Cipher cipher;
    private KeyStore keyStore;
    private KeyGenerator keyGenerator;
    private FingerprintManager.CryptoObject cryptoObject;

    private TextView textView;
    private Button auth_button,stop_button;
    private FingerprintManager fingerprintManager;
    private KeyguardManager keyguardManager;

    @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) getSystemService(KEYGUARD_SERVICE);
            fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
            textView=(TextView)findViewById(R.id.authStatus);
            auth_button=(Button)findViewById(R.id.auth_button);
            stop_button=(Button)findViewById(R.id.stop_button);

            if (!fingerprintManager.isHardwareDetected()) {
                textView.setText("Your device doesn't support fingerprint authentication");
            }

            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
                textView.setText("Please enable the fingerprint permission");
            }

            if (!fingerprintManager.hasEnrolledFingerprints()) {
                textView.setText("No fingerprint configured. Please register at least one fingerprint in your device's Settings");
            }

            if (!keyguardManager.isKeyguardSecure()) {
                textView.setText("Please enable lockscreen security in your device's Settings");
            }

            else {
                auth_button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this, "Starting service", Toast.LENGTH_SHORT).show();
                        try
                        {
                            generateKey();
                        }
                        catch(Exception e)
                        {
                            e.printStackTrace();
                        }
                        if(initCipher())
                        {
                            Provider provider=new Provider(fingerprintManager,cryptoObject,MainActivity.this);
                            Intent intent=new Intent(MainActivity.this,AsyncService.class);
                            startService(intent);
                        }
                    }
                });

                stop_button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        stopService(new Intent(MainActivity.this,AsyncService.class));
                        Toast.makeText(MainActivity.this, "Service stopped", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    }

    private void generateKey() {
        try
        {
            keyStore = KeyStore.getInstance("AndroidKeyStore");
            keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
            keyStore.load(null);
            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)
        {
            e.printStackTrace();
        }
    }

    public boolean initCipher() {
        try
        {
            cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
        }
        catch (Exception e)
        {
            throw new RuntimeException("Failed to get Cipher", e);
        }

        try
        {
            keyStore.load(null);
            SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            return true;
        }
        catch (KeyPermanentlyInvalidatedException e)
        {
            return false;
        }
        catch (Exception e)
        {
            throw new RuntimeException("Failed to init Cipher", e);
        }
    }
}

AsyncService.java

public class AsyncService extends IntentService {

    private int ONGOING_NOTIFICATION_ID=2346712;

    public AsyncService() {
        super(AsyncService.class.getName());
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        showNotification();
        new FingerprintHandler().startAuth(Provider.fpManager,Provider.cryptoObj);
    }

    public void showNotification() {

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent =
                PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Notification notification =
                new Notification.Builder(this)
                        .setContentTitle(getText(R.string.notification_title))
                        .setContentText(getText(R.string.notification_message))
                        .setSmallIcon(R.drawable.launcher)
                        .setContentIntent(pendingIntent)
                        .build();

        startForeground(ONGOING_NOTIFICATION_ID, notification);
    }

    public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {

        private CancellationSignal cancellationSignal;
        public void startAuth(FingerprintManager manager, FingerprintManager.CryptoObject cryptoObject)
        {
            cancellationSignal = new CancellationSignal();
            if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED)
            {
                return;
            }
            manager.authenticate(cryptoObject, cancellationSignal, 0, this, null);
        }

        @Override
        public void onAuthenticationError(int errMsgId, CharSequence errString)
        {

        }

        @Override
        public void onAuthenticationFailed()
        {
           //some action to perform
        }

        @Override
        public void onAuthenticationHelp(int helpMsgId, CharSequence helpString)
        {

        }

        @Override
        public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result)
        {
            //some action to perform
        }
    }
}

Provider.java

public class Provider {

    public static FingerprintManager fpManager;
    public static FingerprintManager.CryptoObject cryptoObj;
    public static Context mContext;

    public Provider(FingerprintManager fingerprintManager, FingerprintManager.CryptoObject cryptoObject, Context context) {
        fpManager=fingerprintManager;
        cryptoObj=cryptoObject;
        mContext=context;
    }
}

【问题讨论】:

  • 你有什么解决办法吗? @apk009

标签: android intentservice android-fingerprint-api


【解决方案1】:

你可以试试下面的代码。首先你需要在 Manifest 文件中添加 Service 的属性

  <service
        android:name=".service.Service"
        android:enabled="true"
        android:icon="@drawable/ic_launcher"
        android:isolatedProcess="true">

  </service>

并在您的服务中添加START_STICKY

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

【讨论】:

    【解决方案2】:

    试试这个, 为我工作

    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
    
        startForeground(ONGOING_NOTIFICATION_ID, showNotification());
        new FingerprintHandler().startAuth(Provider.fpManager,Provider.cryptoObj);
        return START_STICKY;
    }
    
    @Override
    protected void onHandleIntent(Intent intent) {
    
    }
    
    public Notification showNotification() {
    
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent =
                PendingIntent.getActivity(this, 0, notificationIntent, 0);
    
        Notification notification =
                new Notification.Builder(this)
                        .setContentTitle(getText(R.string.notification_title))
                        .setContentText(getText(R.string.notification_message))
                        .setSmallIcon(R.drawable.launcher)
                        .setContentIntent(pendingIntent)
                        .build();
        notificationChannel().notify(ONGOING_NOTIFICATION_ID, notification);
        return notification;
    
    }
    
    @TargetApi(Build.VERSION_CODES.O)
    private NotificationManager notificationChannel() {
        NotificationManager notificationMgr =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    
            NotificationChannel notificationChannel =(NotificationChannel) new NotificationChannel("channel_name", BuildConfig.APPLICATION_ID, NotificationManager.IMPORTANCE_MIN);
            notificationChannel.setDescription("desc");
            notificationChannel.setLightColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
            notificationChannel.enableLights(false);
            notificationChannel.enableVibration(false);
            notificationMgr.createNotificationChannel(notificationChannel);
        }
        return notificationMgr;
    
    }
    

    【讨论】:

      【解决方案3】:

      看我的回答here

      最后,经过几个小时的尝试,我发现了问题,解决方案是你必须创建AccessibilityService并从设置中启用权限,启用权限后,回调将起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多