我是Android开发的新手。目前正在开发扫描二维码的基本Android应用程序。对于演示,我得到了源代码。但是当我试图运行应用程序时。它需要从Playstore下载Barcode Scanner应用程序。我需要做出改变的地方。谢谢
public class AndroidBarcodeQrExample extends Activity {
static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set the main content layout of the Activity
setContentView(R.layout.activity_main);
}
//product barcode mode
public void scanBar(View v) {
try {
//start the scanning activity from the com.google.zxing.client.android.SCAN intent
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException anfe) {
//on catch, show the download dialog
showDialog(AndroidBarcodeQrExample.this, "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
}
}
//product qr code mode
public void scanQR(View v) {
try {
//start the scanning activity from the com.google.zxing.client.android.SCAN intent
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException anfe) {
//on catch, show the download dialog
showDialog(AndroidBarcodeQrExample.this, "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
}
}
//alert dialog for downloadDialog
private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo) {
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
downloadDialog.setTitle(title);
downloadDialog.setMessage(message);
downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try {
act.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
}
}
});
downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return downloadDialog.show();
}
//on ActivityResult method
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
//get the extras that are returned from the intent
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Toast toast = Toast.makeText(this, "Content:" + contents + " Format:" + format, Toast.LENGTH_LONG);
toast.show();
}
}
}
}
答案 0 :(得分:1)
为什么我需要安装条形码扫描仪才能运行此Android应用
您的代码正在从条形码扫描仪应用程序启动活动。因此,您需要安装条形码扫描仪应用程序才能使startActivityForResult()
次呼叫正常工作。
我需要进行更改
您需要删除上面代码清单中的所有内容。然后,您需要找到符合您需求的a barcode-scanning library。
答案 1 :(得分:1)
static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
Intent intent = new Intent(ACTION_SCAN);
以上行意味着,您正在为该指定操作创建意图。这是谷歌的xzing应用程序包中的活动。
因此需要特定的应用程序。这是android的力量。您只需指定ACTION_DIAL
等即可轻松通过应用程序调用来电.Android将根据其处理的内容(由清单公开)调用正确的应用。
假设您有两个拨号器应用,您将可以选择选择哪个。
在您的情况下,由于您使用zxing,预计特定的应用程序将出现。请记住,您不是在编写整个条形码应用程序,只是调用它们的操作。