这是我的代码 我正在寻找一些使用ZXing扫描仪的Android应用程序的帮助。如果结果是URL,我希望它重定向到一个网站。提前谢谢。
以下是我的代码:
公共类MainActivity扩展了AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button);
final Activity activity = this;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
public void handleResult(Result result) {
if(Patterns.WEB_URL.matcher(result.getText).matches()) {
// Open URL
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(result.getText()));
startActivity(browserIntent);
}
}
}
}
}
答案 0 :(得分:0)
这是需要检查的条件:
if(android.util.Patterns.WEB_URL.matcher(result.getContents().trim()).matches() || result.getContents().trim().contains("www.")){
// code to redirect to webview
launchURL(result.getContents().trim())
}
要重定向到webview:
<强> 1&GT;使用自定义标签
添加到您的gradle
implementation 'com.android.support:customtabs:27.0.2'
调用此函数重定向
void launchURL(String url){
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(mContext, Uri.parse(url));
builder.setToolbarColor(ContextCompat.getColor(mContext,R.color.colorPrimary));
}
或强>
<强> 2 - ;使用意向
if (!result.getContents().trim().contains("http")) {
result.getContents().trim()= "http://" + url;
}
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(result.getContents().trim()));
答案 1 :(得分:0)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
handleResult(result);
}
}
public void handleResult(Result result) {
if (Patterns.WEB_URL.matcher(result.getText).matches()) {
// Open URL
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(result.getText()));
startActivity(browserIntent);
}else{
Toast.makeText(this, "Not Match", Toast.LENGTH_LONG).show();
}
}