com.cihon.androidrestart_keven.activity.WebViewActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.cihon.androidrestart_keven.activity.WebViewActivity.java

Source

package com.cihon.androidrestart_keven.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;

import com.cihon.androidrestart_keven.R;
import com.cihon.androidrestart_keven.camera.CameraActivity;
import com.cihon.androidrestart_keven.camera.CameraSurfaceView;

import static com.cihon.androidrestart_keven.util.Constant.REQUEST_CAMERA;

public class WebViewActivity extends AppCompatActivity implements CameraSurfaceView.TransferPhoto {

    private WebView mWebView;
    String s = "";
    public static WebViewActivity instance = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        instance = this;
        //        Button bt = (Button) findViewById(R.id.bt);
        //
        //        bt.setOnClickListener(new View.OnClickListener() {
        //            @Override
        //            public void onClick(View v) {
        //                String url = "javascript:" + "getWord('" + s + "')";
        //                mWebView.loadUrl(url);
        //            }
        //        });

        mWebView = (WebView) findViewById(R.id.webview);
        initWebView();

    }

    /**
     * ?webview
     */
    public void initWebView() {
        MyWebViewClient webViewClient = new MyWebViewClient();
        mWebView.setWebViewClient(webViewClient);
        WebSettings webSettings = mWebView.getSettings();

        //??
        webSettings.setLoadWithOverviewMode(true);
        //?webview?
        webSettings.setUseWideViewPort(true);
        //??
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);
        webSettings.setJavaScriptEnabled(true);

        //        mWebView.loadUrl("http://webservice.cihon.cn/onStarActive1/reupload.html");
        mWebView.loadUrl("http://123.57.60.91/onStar3/vehicle-License.html");
        //        mWebView.loadUrl("http://wxpay.wxutil.com/mch/pay/h5.v2.php");

        //        jsjava
        mWebView.addJavascriptInterface(new JsInterface(this), "demo");
    }

    /**
     * @param str h5??
     */
    @Override
    public void imgBase64(String str, Bitmap b) {

        Log.e("Log", "webview--" + (mWebView == null));
        Log.e("Log", "webview?--" + str);
        String url = "javascript:" + "getWord('" + str + "')";
        mWebView.loadUrl(url);

        ImageView iv = (ImageView) findViewById(R.id.img);
        iv.setImageBitmap(b);

    }

    @Override
    public void finish() {
        ViewGroup view = (ViewGroup) getWindow().getDecorView();
        view.removeAllViews();
        super.finish();
    }

    private class JsInterface {
        private Context mContext;

        public JsInterface(Context context) {
            this.mContext = context;
        }

        //jswindow.demo.takePhoto()?
        //?
        @JavascriptInterface
        public void takePhoto() {
            Log.e("Log", "h5");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                int permissionCheck = ContextCompat.checkSelfPermission(WebViewActivity.this,
                        Manifest.permission.CAMERA);
                if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                    //                if(!ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA)) {
                    //                    Toast.makeText(context,"?SD?????",Toast.LENGTH_SHORT).show();
                    //                    return;
                    //                }
                    ActivityCompat.requestPermissions(WebViewActivity.this,
                            new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                    Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS },
                            REQUEST_CAMERA);
                    return;
                }
                startActivity(new Intent(WebViewActivity.this, CameraActivity.class));
            } else {
                startActivity(new Intent(WebViewActivity.this, CameraActivity.class));
            }
        }

    }
}