打开链接在 Android Studio 中的 Webview。

huangapple go评论74阅读模式
英文:

open the link in webview Android Studio

问题

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scanner);
    scannView = findViewById(R.id.scannerView);
    codeScanner = new CodeScanner(this, scannView);
    resultData = findViewById(R.id.resultsOfQr);

    codeScanner.setDecodeCallback(new DecodeCallback() {
        @Override
        public void onDecoded(@NonNull final Result result) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    resultData.setText(result.getText());

                    Intent intent = new Intent(getBaseContext(), MainActivity.class);
                    intent.putExtra("myurl1", result.getText());
                    startActivity(intent);
                }
            });

        }
    });

    scannView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            codeScanner.startPreview();
        }
    });
}
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    scanBtn = findViewById(R.id.scanBtn);

    MywebView = (WebView) findViewById(R.id.view2);
    WebSettings webSettings = MywebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    MywebView.loadUrl("https://google.com/APP/");

    MywebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("tel:")) {
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                return true;
            } else {
                return false;
            }
        }
    });
    scanBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), Scanner.class));
        }
    });

    String myrul = getIntent().getStringExtra("myurl1");
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            MywebView.loadUrl(myrul);
        }
    });
}

希望这些翻译能够满足您的需求。如果有其他问题,请随时问我。

英文:

hello i have created the webview in mail activity of the android studio and now i am in scanner activity and when the APP scans any QR code having the link it just writs the link but what i want to do is to open the link in the web view already created in mail activity.

let me place my scanner file here

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanner);
        scannView = findViewById(R.id.scannerView);
        codeScanner = new CodeScanner(this,scannView);
        resultData = findViewById(R.id.resultsOfQr);

        codeScanner.setDecodeCallback(new DecodeCallback() {
            @Override
            public void onDecoded(@NonNull final Result result) {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        resultData.setText(result.getText());
//                        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(result.getText()));
//                        startActivity(browserIntent);

                        Intent intent = new Intent(getBaseContext(), MainActivity.class);
                        intent.putExtra("myurl1", result.getText());
                        startActivity(intent);

                    }
                });

            }
        });


        scannView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                codeScanner.startPreview();
            }
        });
    }
    
    
            scannView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    codeScanner.startPreview();
                }
            });
        }

WEBVIEW in Mailactivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    scanBtn = findViewById(R.id.scanBtn);

    MywebView = (WebView) findViewById(R.id.view2);
    WebSettings webSettings = MywebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    MywebView.loadUrl("https://google.com/APP/");

    MywebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("tel:")) {
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                return true;
            }else{
                return false;
            }
        }
    });
    scanBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(),Scanner.class));
        }
    });

    String myrul = getIntent().getStringExtra("myurl1");
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            MywebView.loadUrl(myrul);
        }
    });

}

Any idea how can i do this, any help is appreciated.
Thankx

答案1

得分: 0

在你的 CodeScanner 回调中加载 WebView

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        resultData.setText(result.getText());
        MywebView.loadUrl(result.getText());
    }
});
英文:

In you CodeScanner Callback Load the WebView

runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    resultData.setText(result.getText());
                    MywebView.loadUrl(result.getText());
                }
            });

huangapple
  • 本文由 发表于 2020年9月21日 23:50:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63995753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定