我正在开发一个内置二维码阅读器的应用程序,扫描完代码后,我必须启动另一个活动(名为CodaActivity.class),并从qrcode中获取一个参数。
我从这里找到的一个教程开始:(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();
}
}但在我扫描二维码(当然是使用物理设备)之后,debbugger退出,并显示以下消息:
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.手机中的应用程序仍然处于打开状态,但会转到不同的活动。为什么?以及为什么我没有得到错误!感谢您的回复
发布于 2019-05-29 16:02:25
经过多次测试和使用另一部手机(使用oreo),我最终发现拆分字符串是错误的,令牌是错误的,但对于前一部手机(android 6)没有抛出异常,所以我无法弄清楚发生了什么。此外,更仔细地查看代码,我在startActivity ()调用之前插入了finish ()语句...
我希望这能帮助到一些人。
发布于 2021-04-18 12:47:33
我希望这能帮助到一些人,我花了更多的时间来找出你应该使用正确的版本:^1.0.1,并将其添加到pupspec.yaml中,如下所示:
dependencies:
flutter:
sdk: flutter
flutter_barcode_scanner: ^1.0.1https://stackoverflow.com/questions/56347019
复制相似问题