1

バックグラウンド

指紋認証を追加したい小さなアプリがあります。この認証は、アプリのロックを解除するためにのみ使用されます。

重要なことは次のとおりです。

  • アプリの要点は、サービスとしてバックグラウンドで実行することです。
  • ユーザーは、(アクティビティからだけでなく) システム内のどこからでもアプリのロックを解除できる必要があります。
  • ユーザーは、サービスを開始するために通知をタッチする必要があるため、指紋認証を開始します。

問題

フォアグラウンドから開始するFingerprintService必要がありますActivitystartForeground()でサービスをフォアグラウンドに置いても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;
}

質問

FingerprintServiceフォアグラウンドで実行されているカスタム サービスからを使用するにはどうすればよいですか? 別のアプリがこれを行っているのを見たので、それが可能であることはわかっています。

4

1 に答える 1

1

通知により、指紋センサーを実行するアクティビティが起動され、サービスが開始されます。

于 2017-01-02T00:23:25.107 に答える