我正在开发一个带有嵌入式QR码阅读器的应用程序,在扫描了代码之后,我必须使用从qrcode提取的参数来启动另一个活动(名为CodaActivity.class)。
我从此处的教程开始:(https://www.androidtutorialonline.com/android-qr-code-scanner/) 然后我尝试根据自己的需要对其进行自定义。
这是QRCodeScannerActivity的代码:
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
import static android.Manifest.permission.CAMERA;
public class QrCodeScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private static final int REQUEST_CAMERA = 1;
private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (checkPermission()) {
Toast.makeText(getApplicationContext(), "Permission already granted", Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
}
private boolean checkPermission() {
return ( ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA ) == PackageManager.PERMISSION_GRANTED);
}
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{CAMERA}, REQUEST_CAMERA);
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CAMERA:
if (grantResults.length > 0) {
boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (cameraAccepted){
Toast.makeText(getApplicationContext(), "Permission Granted, Now you can access camera", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access and camera", Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(CAMERA)) {
showMessageOKCancel("You need to allow access to both the permissions",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{CAMERA},
REQUEST_CAMERA);
}
}
});
return;
}
}
}
}
break;
}
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new android.support.v7.app.AlertDialog.Builder(QrCodeScannerActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
@Override
public void onResume() {
super.onResume();
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (checkPermission()) {
if(mScannerView == null) {
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
}
mScannerView.setResultHandler(this);
mScannerView.startCamera();
} else {
requestPermission();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
mScannerView.stopCamera();
}
@Override
public void handleResult(Result rawResult) {
final String testoLink = rawResult.getText();
String[] risultato = testoLink.split(":");
risultato[1] = risultato[1].trim();
Log.e("QRCodeScanner", rawResult.getText());
Intent intent = new Intent(QrCodeScannerActivity.this, CodaActivity.class);
Bundle b = new Bundle();
b.putString("medico", risultato[1]);
intent.putExtras(b);
startActivity(intent);
finish();
}
}
但是,当我扫描QR码(当然是使用物理设备)后,调试器退出并显示以下消息:
I/art: Object allocation is busy now, so prior to grow the heap. New heap size is 33 MB
I/art: current process_level is : 0
I/art: current process_level is : 0
I/art: current process_level is : 0
I/art: current process_level is : 0
I/art: current process_level is : 0
I/Process: Sending signal. PID: 8310 SIG: 9
Application terminated.
手机中的应用仍保持运行状态,但执行其他活动。 为什么?以及为什么我没有错误! 感谢您的回答
答案 0 :(得分:0)
经过多次尝试,并在另一部带奥利奥(Oreo)的电话上,我终于发现我在分割字符串时犯了一个错误,令牌是错误的,但是在先前的电话中没有异常,因此我无法弄清楚这个证据。并更好地观看代码,比起startActivity()调用,我将finish()语句放在前面。
希望这可以帮助某人。
答案 1 :(得分:0)
我希望这可以帮助某人,我需要更多时间来找出它 您应该使用正确的版本:^1.0.1 并将其添加到 pupspec.yaml 中,如下所示:
dependencies:
flutter:
sdk: flutter
flutter_barcode_scanner: ^1.0.1