将qr值发送到webview加载网址

时间:2019-01-26 13:13:34

标签: android webview

我不想传递文本结果,而是传递给webview活动loadurl

这是我的扫描仪活动

package com.project.ciisapp.ciis;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Vibrator;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;

import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;

import java.io.IOException;

public class scannner extends AppCompatActivity {

SurfaceView cameraPreview;
TextView txtResult;
BarcodeDetector barcodeDetector;
CameraSource cameraSource;
final int RequestCameraPermissionID = 1001;


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case RequestCameraPermissionID: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                try {
                    cameraSource.start(cameraPreview.getHolder());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        break;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cameraPreview = (SurfaceView) findViewById(R.id.cameraPreview);
    txtResult = (TextView) findViewById(R.id.txtResult);

    barcodeDetector = new BarcodeDetector.Builder(this)
            .setBarcodeFormats(Barcode.QR_CODE)
            .build();
    cameraSource = new CameraSource
            .Builder(this, barcodeDetector)
            .setAutoFocusEnabled(true)
            .setRequestedPreviewSize(1920, 1080)
            .build();
    //Add Event
    cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                //Request permission
                ActivityCompat.requestPermissions(scannner.this,
                        new String[]{Manifest.permission.CAMERA},RequestCameraPermissionID);
                return;
            }
            try {
                cameraSource.start(cameraPreview.getHolder());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            cameraSource.stop();

        }
    });

    barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
        @Override
        public void release() {

        }

        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> qrcodes = detections.getDetectedItems();
            if(qrcodes.size() != 0)
            {
              Intent intent = new Intent(scannner.this, Main2Activity.class);
                startActivityForResult(intent, 100);
                intent.putExtra("mUrl", qrcodes.valueAt(0));
                finish();
                    }
                });
            }
        }
    });
}
}

这是我的网络视图活动

package com.project.ciisapp.ciis;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class Main2Activity extends AppCompatActivity {
    private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    String url  = getIntent().getStringExtra("mUrl");
    if (url == null || url.isEmpty()) finish();

    webView= (WebView)findViewById(R.id.wv);
    webView.setWebChromeClient(new WebChromeClient());
    webView.loadUrl(url);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
}
@Override
public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    }else {
        super.onBackPressed();
    }
}
}

我一直在Google中搜索教程,但找不到一个。而不是在txtresult中显示链接,该链接将被发送到webviewactivity,然后将该链接加载为匹配父项,然后单击onbackpress它将返回扫描器活动。

更新

我尝试使用意图并将其发送到另一个活动,但没有显示

0 个答案:

没有答案