NFC 在相机意图后停止在设备上工作



我正在开发一个可以拍照和扫描NFC标签的应用程序。NFC标签扫描工作正常,直到我拍照(通过相机意图(。

我做什么:

在 onResume(( 中,我调用 setupForegroundDispatch((,在 onPause(( 中,我调用 stopForegroundDispatch((。

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
final Intent intent = new Intent(activity, activity.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
adapter.enableForegroundDispatch(activity, pendingIntent, null, null);
}
public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
adapter.disableForegroundDispatch(activity);
}

要启动相机,我调用:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
imageCaptureUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);

总结一下:

  • 活动开始,在 onResume(( setupForgroundDispatch(( 中将被调用。
  • NFC 标签的扫描按预期工作
  • 我单击按钮以通过 Intent 启动相机
    • 如果我不拍照并回到我的活动 -> NFC 效果很好
    • 如果我拍照并且onActivityResult((将被调用->NFC在大约20-30秒内不起作用,然后再次开始工作。我的意思是NFC停止在整个设备上工作,而不仅仅是在应用程序中。我可以拍照,然后继续我的活动,然后从最近的应用程序中删除该应用程序,NFC 在提到的 20-30 秒内不起作用。然后再次开始工作。

我尝试了什么

我检查了 stopForegroundDispatch(( 将在相机
  • 启动之前调用,并且 setupForegroundDispatch(( 将在相机完成后调用
  • 我从onActivityResult((中删除了我的代码,以确保任何进一步的代码都会干扰NFC。

我想你可能遇到过这个

打开相机时 NFC 不可用

也许您应该尝试使用其他设备(如果有的话(

最新更新