我一直在我的 Android Java 项目中使用“com.google.zxing.integration.android.IntentIntegrator”,如下所示:
IntentIntegrator integrator = new IntentIntegrator(QrCodeActivity.this);
既然此类已弃用,我如何在项目中替换此功能?
我一直在寻找解决方案,但还没有找到。如果可以的话,我想继续使用ZXing。
正如他们在已弃用的类中所描述的:“使用 ScanOptions 和 ScanContract 来代替”,就像这样:
在 onCreate() 中初始化意图:
private ActivityResultLauncher<Intent> scanQrResultLauncher;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scanQrResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
resultData ->{
if (resultData.getResultCode() == RESULT_OK) {
ScanIntentResult result = ScanIntentResult.parseActivityResult(resultData.getResultCode(), resultData.getData());
//this will be qr activity result
if (result.getContents() == null) {
Toast.makeText(getContext(), getString(R.string.canceled), Toast.LENGTH_LONG).show();
} else {
String qrContents = result.getContents();
//TODO Handle qr result here
}
}
});
}
当您想启动扫描仪时:
scanQrResultLauncher.launch(new ScanContract().createIntent(getContext(), new ScanOptions()));