2017-10-10 112 views
0

我使用android studio 2.3.3开发了一个使用android webview的网站,我的问题是加载屏幕不起作用。无法在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); 
} 
} 

我转换为网站与Android应用采用了android studio2.3.3,努力文件浏览选项,但不能显示加载屏幕,请帮帮我,我 开发网站使用android studio 2.3.3 android webview,我的问题是加载屏幕无法正常工作,请帮助我。

回答

1

除了@KeLiuyue's answer,您还应该合并您的两个WebChromeClient。删除此代码:

myWebView.setWebChromeClient(new WebChromeClient() { 
    @Override 
    public void onGeolocationPermissionsShowPrompt(String origin, 
     GeolocationPermissions.Callback callback) { 
     callback.invoke(origin, true, false); 
    } 
}); 

,并添加AttWebChromeClient覆盖onGeolocationPermissionsShowPrompt的方法。

+0

谢谢您的补充。我为你赞成。 – KeLiuyue

1

1.更改

myWebView.setWebViewClient(new Callback()); 

myWebView.setWebViewClient(new Client()); 

2.加Callback方法Client

的Bec因为你有两个课程范围WebViewClient,所以你可以留下一个课。将另一个类的数据放入左边的类中。