gpt4 book ai didi

javascript - Android Studio Zxing Scanner,如果QR码的结果是URL,如何重定向到网页?

转载 作者:行者123 更新时间:2023-11-29 02:30:54 26 4
gpt4 key购买 nike

这是我的代码我正在寻找有关使用 ZXing 扫描仪的 Android 应用程序的帮助。如果结果是 URL,我希望它重定向到一个网站。提前致谢。

以下是我的代码:

公共(public)类 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);
}
}

}
}

最佳答案

这是要检查的条件:

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> 使用自定义标签

Add to your gradle

implementation 'com.android.support:customtabs:27.0.2'

Call this function to redirect

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>使用 Intent

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()));

关于javascript - Android Studio Zxing Scanner,如果QR码的结果是URL,如何重定向到网页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49666045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com