我正在开发一个带有嵌入式二维码阅读器的应用程序,扫描代码后,我必须使用从二维码获取的参数启动另一个 Activity (名为 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();
}
}
但是在我扫描二维码(当然是使用物理设备)后,调试器退出并显示以下消息:
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.
手机中的应用程序仍然保持运行,但转到不同的 Activity 。 为什么?以及为什么我没有错误! 感谢您的回答
最佳答案
经过多次测试并使用另一部手机(带有oreo),我终于发现我错误地分割了字符串, token 是错误的,但使用之前的手机(android 6)没有抛出异常,所以我无法弄清楚知道发生了什么。此外,更仔细地查看代码,我在 startActivity() 调用之前插入了 finish() 语句...
我希望这可以帮助别人。
关于java - 发送信号。 PID : 6939 SIG: 9 with zxing QR code scanner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56347019/