Java 无法在android webview中加载屏幕?

Java 无法在android webview中加载屏幕?,java,android,xml,webview,Java,Android,Xml,Webview,我用android studio 2.3.3开发了一个带有android webview的网站,我的问题是加载屏幕不起作用 这是我的java代码 public class MainActivity extends ActionBarActivity { private static final String TAG = MainActivity.class.getSimpleName(); private WebView myWebView; private WebSettings webSet

我用android studio 2.3.3开发了一个带有android webview的网站,我的问题是加载屏幕不起作用

这是我的java代码

public class MainActivity extends ActionBarActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private WebView myWebView;
private WebSettings webSettings;
private String mCM;
private ValueCallback<Uri> mUM;
private ValueCallback<Uri[]> mUMA;
private final static int FCR=1;
private static final int REQUEST_CODE_PERMISSION = 1;
String mPermission = Manifest.permission.ACCESS_FINE_LOCATION;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    super.onActivityResult(requestCode, resultCode, intent);
    if(Build.VERSION.SDK_INT >= 21){
        Uri[] results = null;
        //Check if response is positive
        if(resultCode== Activity.RESULT_OK){
            if(requestCode == FCR){
                if(null == mUMA){
                    return;
                }
                if(intent == null){
                    if(mCM != null){
                        results = new Uri[]{Uri.parse(mCM)};
                    }
                }else{
                    String dataString = intent.getDataString();
                    if(dataString != null){
                        results = new Uri[]{Uri.parse(dataString)};
                    }
                }
            }
        }
        mUMA.onReceiveValue(results);
        mUMA = null;
    }else{
        if(requestCode == FCR){
            if(null == mUM) return;
            Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
            mUM.onReceiveValue(result);
            mUM = null;
        }
    }
}
@SuppressLint({"SetJavaScriptEnabled", "WrongViewCast"})

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //attachments
    if(Build.VERSION.SDK_INT >=23 && (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
    }
    myWebView = (WebView) findViewById(R.id.webView);
    assert myWebView != null;
    webSettings = myWebView.getSettings();
    myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    myWebView.getSettings().setBuiltInZoomControls(true);
    myWebView.setWebViewClient(new Client());
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.getSettings().setGeolocationEnabled(true);
    myWebView.getSettings().setAppCacheEnabled(true);
    myWebView.getSettings().setDatabaseEnabled(true);
    myWebView.getSettings().setDomStorageEnabled(true);
    webSettings.setGeolocationEnabled(true);
    if(Build.VERSION.SDK_INT >= 21){
        webSettings.setMixedContentMode(0);
        myWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    }else if(Build.VERSION.SDK_INT >= 19){
        myWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    }else if(Build.VERSION.SDK_INT < 19){
        myWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    myWebView.setWebViewClient(new Callback());
    myWebView.setWebChromeClient(new AttWebChromeClient());        myWebView.loadUrl("http://www.webrivers.co.in/green_medic/lock_screen.html"); 
   myWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, false);
        }
    });
     }

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    return false;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    return super.onOptionsItemSelected(item);
}
public class Client extends WebViewClient {
    ProgressDialog progressDialog;

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.contains("mailto:")) {
            view.getContext().startActivity(
                    new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        } else {
            view.loadUrl(url);
            return true;
        }
    }

     public void onPageStarted(WebView view, String url, Bitmap favicon) {

        if (progressDialog == null) {
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setMessage("Loading...");
            progressDialog.show();
        }
    }        
    public void onPageFinished(WebView view, String url) {
        try {                
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
                progressDialog = null;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}
public class AttWebChromeClient extends WebChromeClient {

    public void openFileChooser(ValueCallback<Uri> uploadMsg){
        mUM = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");
        MainActivity.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FCR);
    }

    public void openFileChooser(ValueCallback uploadMsg, String acceptType){
        mUM = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");
        MainActivity.this.startActivityForResult(
                Intent.createChooser(i, "File Browser"),
                FCR);
    }

    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
        mUM = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");
        MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FCR);
    }

    public boolean onShowFileChooser(
            WebView webView, ValueCallback<Uri[]> filePathCallback,
            WebChromeClient.FileChooserParams fileChooserParams){
        if(mUMA != null){
            mUMA.onReceiveValue(null);
        }
        mUMA = filePathCallback;
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if(takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null){
            File photoFile = null;
            try{
                photoFile = createImageFile();
                takePictureIntent.putExtra("PhotoPath", mCM);
            }catch(IOException ex){
                Log.e(TAG, "Image file creation failed", ex);
            }
            if(photoFile != null){
                mCM = "file:" + photoFile.getAbsolutePath();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            }else{
                takePictureIntent = null;
            }
        }
        Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
        contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
        contentSelectionIntent.setType("*/*");
        Intent[] intentArray;
        if(takePictureIntent != null){
            intentArray = new Intent[]{takePictureIntent};
        }else{
            intentArray = new Intent[0];
        }

        Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
        chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
        chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
        startActivityForResult(chooserIntent, FCR);
        return true;
    }

}

public class Callback extends WebViewClient{
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
        Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
    }
}

private File createImageFile() throws IOException{
    @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "img_"+timeStamp+"_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(imageFileName,".jpg",storageDir);
}
@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
}
}
公共类MainActivity扩展了ActionBarActivity{
私有静态最终字符串标记=MainActivity.class.getSimpleName();
私有网络视图myWebView;
私人网站设置网站设置;
私有字符串mCM;
私人财产;
私人资产管理公司;
专用最终静态整数FCR=1;
私有静态最终整数请求\代码\权限=1;
字符串mPermission=Manifest.permission.ACCESS\u FINE\u位置;
@凌驾
ActivityResult上受保护的void(int-requestCode、int-resultCode、Intent-Intent){
super.onActivityResult(请求代码、结果代码、意图);
如果(Build.VERSION.SDK_INT>=21){
Uri[]results=null;
//检查响应是否为阳性
if(resultCode==Activity.RESULT\u确定){
if(requestCode==FCR){
if(null==mUMA){
返回;
}
if(intent==null){
如果(mCM!=null){
结果=新Uri[]{Uri.parse(mCM)};
}
}否则{
字符串dataString=intent.getDataString();
if(数据字符串!=null){
结果=新Uri[]{Uri.parse(dataString)};
}
}
}
}
mUMA.onReceiveValue(结果);
mUMA=null;
}否则{
if(requestCode==FCR){
if(null==mUM)返回;
Uri result=intent==null | | resultCode!=result_OK?null:intent.getData();
最小接收值(结果);
mUM=null;
}
}
}
@SuppressLint({“SetJavaScriptEnabled”,“ErrorViewCast”})
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//附件
if(Build.VERSION.SDK_INT>=23&&(ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.permission|授予ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA)!=PackageManager.permission|授予ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS.ACCESS|位置)!=PackageManager.PERMISSION\u已授予| | ContextCompat.checkSelfPermission(此,Manifest.PERMISSION.ACCESS\u位置)!=PackageManager.PERMISSION\u已授予){
ActivityCompat.requestPermissions(MainActivity.this,新字符串[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_rough_LOCATION},1);
}
myWebView=(WebView)findViewById(R.id.WebView);
断言myWebView!=null;
webSettings=myWebView.getSettings();
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().SetBuilTinZoomControl(true);
setWebViewClient(新客户端());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setGeolocationEnabled(true);
myWebView.getSettings().setAppCacheEnabled(true);
myWebView.getSettings().setDatabaseEnabled(true);
myWebView.getSettings().setDomStorageEnabled(true);
webSettings.setGeolocationEnabled(true);
如果(Build.VERSION.SDK_INT>=21){
webSettings.setMixedContentMode(0);
myWebView.setLayerType(View.LAYER\u TYPE\u硬件,空);
}else if(Build.VERSION.SDK_INT>=19){
myWebView.setLayerType(View.LAYER\u TYPE\u硬件,空);
}else if(Build.VERSION.SDK_INT<19){
myWebView.setLayerType(View.LAYER\u TYPE\u软件,空);
}
setWebViewClient(新回调());
myWebView.setWebChromeClient(新的AttWebChromeClient());myWebView.loadUrl(“http://www.webrivers.co.in/green_medic/lock_screen.html"); 
myWebView.setWebChromeClient(新WebChromeClient(){
@凌驾
public void onGeolocationPermissionsShowPrompt(字符串来源,GeolocationPermissions.Callback){
callback.invoke(origin、true、false);
}
});
}
公共布尔onKeyDown(int-keyCode,KeyEvent事件){
if((keyCode==KeyEvent.keyCode_BACK)&&myWebView.canGoBack()){
myWebView.goBack();
返回true;
}
返回super.onKeyDown(keyCode,event);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值OnPrepareOptions菜单(菜单){
返回false;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
int id=item.getItemId();
返回super.onOptionsItemSelected(项目);
}
公共类客户端扩展了WebViewClient{
进行对话进行对话;
公共布尔值shouldOverrideUrlLoading(WebView视图,字符串url){
if(url.contains(“mailto:”){
view.getContext().startActivity(
新的意图(Intent.ACTION_视图,Uri.parse(url));
返回true;
}否则{
view.loadUrl(url);
返回true;
}
}
public void onPageStarted(WebView视图、字符串url、位图favicon){
如果(progressDialog==null){
progressDialog=新建progressDialog(MainActivity.this);
progressDialog.setMessage(“加载…”);
progressDialog.show();
}
}        
公共void onPageFinished(WebView视图,字符串url){
试试{
if(progressDialog.isShowing()){
progressDialog.disclose();
progressDialog=null;
}
}捕获(异常){
异常。printStackTrace();
}
}
}
公共类AttWebChromeClient扩展了WebChromeClient{
public void openFileChooser(ValueCallback uploadMsg){
mUM=上传msg;
意向i=新意向(Intent.ACTI
myWebView.setWebViewClient(new Callback());
myWebView.setWebViewClient(new Client());
myWebView.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin, 
        GeolocationPermissions.Callback callback) {
        callback.invoke(origin, true, false);
    }
});