从自定义服务使用 FingerprintService

Use FingerprintService from a custom service

背景

我有一个小应用程序,我想在其中添加指纹验证。此身份验证仅用于解锁应用程序。

重要的事情是:

问题

FingerprintService 必须从前台启动 Activity。即使我使用 startForeground() 将服务置于前台,FingerprintService 也会发出警告:

01-01 23:22:11.015 1576-1576/system_process W/FingerprintService: Rejecting com.package.myapp ; not in foreground
01-01 23:22:11.015 1576-1576/system_process V/FingerprintService: authenticate(): reject com.package.myapp

仅当我在应用程序外部(例如,在主屏幕中)触摸通知时才会发生这种情况。如果我在应用程序中触摸它,则身份验证有效并且我可以使用我的指纹解锁(因为 Acticity 已聚焦,在前景中)。

我当前的代码(只有相关部分)

来自Activity,显示启动服务的通知:

// NofityManager is a factory class that returns a configured Builder
NotificationCompat.Builder builder = NotifyManager.getBuilder(this);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(SERVICE_NOTIFICATION_ID, builder.build());

服务启动后,将其置于前台并启动指纹认证:

@Override
public void onCreate()
{
    NotificationCompat.Builder builder = NotifyManager.getBuilder(this)
        .setContentTitle(getString(R.string.notification_title2))
        .setContentText(getString(R.string.notification_text));

    startForeground(FOREGROUND_NOTIFICATION_ID, builder.build());

    // this is the class that I use to setup the FingerprintService
    // and call authenticate()
    fingerprintValidator = new FingerprintValidator(this);

    if(fingerprintValidator.check(false))
    {
        try
        {
            fingerprintValidator.startService(this);
        }
        catch(RuntimeException ex)
        {
            Log.d(TAG, ex.getLocalizedMessage() + "\n" + ex.getStackTrace().toString());
        }
    }
    else
    {
        // notify the user
    }
}

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

问题

如何在前台使用自定义服务 运行ning 中的 FingerprintService?我知道这是可能的,因为我看到另一个应用程序这样做,所以必须是一种方式。

让您的通知启动运行指纹传感器的 Activity,然后启动您的服务。