1

How do I need to change the HandleResult code for opening the URL within the application and not in the external browser?

I know that we need something like Webview and the layout file also needs to be in Webview. I hope someone can help me? I tried it but it didn't work.

package com.example.beverly.registrationdatabase;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;

import com.google.zxing.Result;

import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class ScanCodeActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {

    ZXingScannerView ScannerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScannerView = new ZXingScannerView(this);
        setContentView(ScannerView);


    }

    @Override
    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);
            onBackPressed();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        ScannerView.stopCamera();
    }

    @Override
    protected void onResume() {
        super.onResume();

        ScannerView.setResultHandler(this);
        ScannerView.startCamera();
    }
}
Jake Lee
  • 7,549
  • 8
  • 45
  • 86
Joe
  • 11
  • 1
  • 2
  • Are you just trying to download content from a URL? Such as this question: https://stackoverflow.com/q/9977221/4851565 – entpnerd Jan 13 '19 at 21:28
  • Thanks for the answer. I don't understand your question. What do you mean with download content from a URL? I think i should tell my problem again. I created an QR Code scanner and if i click on the scan button it scans the qr code and open the link in the browser but not in the application self. – Joe Jan 13 '19 at 21:34
  • Are you just trying to access a web page? – entpnerd Jan 13 '19 at 21:35
  • Of course. Only a web page. – Joe Jan 13 '19 at 21:36
  • The QR Codes which I scan contains only a URL not more. – Joe Jan 13 '19 at 21:42
  • Got it - sounds like a pretty typical use case to me. See answer below. – entpnerd Jan 13 '19 at 21:43

2 Answers2

0

If you are just trying to access content from a standard webpage, consider putting the content directly into a string via IOUtils.toString().

entpnerd
  • 10,049
  • 8
  • 47
  • 68
  • I don't want the content into a string. I only want if that qr code was scanned it opens a new activity in a webview. – Joe Jan 13 '19 at 21:46
  • Ah. If I'm not mistaken, when a new activity has been started, the `onStart()` method should be triggered shouldn't it? Would that method be of use? – entpnerd Jan 13 '19 at 22:06
  • I think i need to complain the whole problem new. So If I click on the Scan Button it opens the qr code scanner, I scan the qr code, the scanner detects a url and open the external webbrowser. So my Problem is I want that the Url open in my Application itself. And i don't know how I need to change the code. – Joe Jan 13 '19 at 22:28
0

Create a new activity with webview in it.

Move your string using intent to a webview Activity and open the url from that webview.

    myWebView = (WebView)findViewById(R.id.webView);
            WebSettings webSettings = myWebView.getSettings();
            myWebView.getSettings().setJavaScriptEnabled(true);
            myWebView.getSettings().setLoadWithOverviewMode(true);
            myWebView.getSettings().setUseWideViewPort(true);
            myWebView.getSettings().setDomStorageEnabled(true);

            myWebView.loadUrl(String_from_qr_scan);

Do not forget to add Internet permission in manifest. Also if you want webview to navigate further pages from the webpage it will open then you will have to override setWebViewClient() method also.

Wijdan
  • 203
  • 3
  • 12