| @@ -2,8 +2,13 @@ | |||||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||||
| package="com.nsgk.ruralWeb"> | package="com.nsgk.ruralWeb"> | ||||
| <uses-permission android:name="android.permission.INTERNET"/> | |||||
| <uses-permission android:name="android.permission.INTERNET" /> | |||||
| <uses-permission android:name="android.permission.CAMERA" /> | |||||
| <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | |||||
| <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |||||
| <application | <application | ||||
| android:requestLegacyExternalStorage="true" | |||||
| android:allowBackup="true" | android:allowBackup="true" | ||||
| android:icon="@mipmap/ic_launcher_white" | android:icon="@mipmap/ic_launcher_white" | ||||
| android:roundIcon="@mipmap/ic_launcher_white_round" | android:roundIcon="@mipmap/ic_launcher_white_round" | ||||
| @@ -0,0 +1,125 @@ | |||||
| package com.nsgk.ruralWeb; | |||||
| import android.annotation.TargetApi; | |||||
| import android.app.Activity; | |||||
| import android.content.Intent; | |||||
| import android.net.Uri; | |||||
| import android.os.Build; | |||||
| import android.webkit.PermissionRequest; | |||||
| import android.webkit.ValueCallback; | |||||
| import android.webkit.WebChromeClient; | |||||
| import android.webkit.WebView; | |||||
| public class BaseWebChromeClient extends WebChromeClient { | |||||
| private static final int CHOOSE_REQUEST_CODE = 0x9001; | |||||
| private Activity mActivity; | |||||
| private ValueCallback<Uri> uploadFile;//定义接受返回值 | |||||
| private ValueCallback<Uri[]> uploadFiles; | |||||
| // private ProgressBar bar; | |||||
| // private TextView mTitle; | |||||
| public BaseWebChromeClient(Activity mActivity) { | |||||
| this.mActivity = mActivity; | |||||
| // this.bar=bar; | |||||
| // this.mTitle=title; | |||||
| } | |||||
| @Override | |||||
| public void onProgressChanged(WebView view, int newProgress) { | |||||
| if (newProgress == 100) { | |||||
| // bar.setVisibility(View.INVISIBLE); | |||||
| } else { | |||||
| // if (View.INVISIBLE == bar.getVisibility()) { | |||||
| // bar.setVisibility(View.VISIBLE); | |||||
| // } | |||||
| // bar.setProgress(newProgress); | |||||
| } | |||||
| super.onProgressChanged(view, newProgress); | |||||
| } | |||||
| @Override | |||||
| public void onReceivedTitle(WebView view, String title) { | |||||
| super.onReceivedTitle(view, title); | |||||
| // mTitle.setText(title); | |||||
| } | |||||
| @TargetApi(Build.VERSION_CODES.LOLLIPOP) | |||||
| @Override | |||||
| public void onPermissionRequest(PermissionRequest request) { | |||||
| // super.onPermissionRequest(request);//必须要注视掉 | |||||
| request.grant(request.getResources()); | |||||
| } | |||||
| // For Android 3.0+ | |||||
| public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { | |||||
| this.uploadFile = uploadFile; | |||||
| openFileChooseProcess(); | |||||
| } | |||||
| // For Android < 3.0 | |||||
| public void openFileChooser(ValueCallback<Uri> uploadMsgs) { | |||||
| this.uploadFile = uploadFile; | |||||
| openFileChooseProcess(); | |||||
| } | |||||
| // For Android > 4.1.1 | |||||
| // @Override | |||||
| public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { | |||||
| this.uploadFile = uploadFile; | |||||
| openFileChooseProcess(); | |||||
| } | |||||
| // For Android >= 5.0 | |||||
| @Override | |||||
| public boolean onShowFileChooser(WebView webView, | |||||
| ValueCallback<Uri[]> filePathCallback, | |||||
| WebChromeClient.FileChooserParams fileChooserParams) { | |||||
| this.uploadFiles = filePathCallback; | |||||
| openFileChooseProcess(); | |||||
| return true; | |||||
| } | |||||
| private void openFileChooseProcess() { | |||||
| Intent i = new Intent(Intent.ACTION_GET_CONTENT); | |||||
| i.addCategory(Intent.CATEGORY_OPENABLE); | |||||
| i.setType("image/*"); | |||||
| mActivity.startActivityForResult(Intent.createChooser(i, "Choose"), CHOOSE_REQUEST_CODE); | |||||
| } | |||||
| public void onActivityResult(int requestCode, int resultCode, Intent data) { | |||||
| if (resultCode == Activity.RESULT_OK) { | |||||
| switch (requestCode) { | |||||
| case CHOOSE_REQUEST_CODE: | |||||
| if (null != uploadFile) { | |||||
| Uri result = data == null || resultCode != Activity.RESULT_OK ? null | |||||
| : data.getData(); | |||||
| uploadFile.onReceiveValue(result); | |||||
| uploadFile = null; | |||||
| } | |||||
| if (null != uploadFiles) { | |||||
| Uri result = data == null || resultCode != Activity.RESULT_OK ? null | |||||
| : data.getData(); | |||||
| uploadFiles.onReceiveValue(new Uri[]{result}); | |||||
| uploadFiles = null; | |||||
| } | |||||
| break; | |||||
| default: | |||||
| break; | |||||
| } | |||||
| } else if (resultCode == Activity.RESULT_CANCELED) { | |||||
| if (null != uploadFile) { | |||||
| uploadFile.onReceiveValue(null); | |||||
| uploadFile = null; | |||||
| } | |||||
| if (null != uploadFiles) { | |||||
| uploadFiles.onReceiveValue(null); | |||||
| uploadFiles = null; | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -1,6 +1,7 @@ | |||||
| package com.nsgk.ruralWeb; | package com.nsgk.ruralWeb; | ||||
| import android.annotation.SuppressLint; | import android.annotation.SuppressLint; | ||||
| import android.content.Intent; | |||||
| import android.os.Bundle; | import android.os.Bundle; | ||||
| import android.view.KeyEvent; | import android.view.KeyEvent; | ||||
| import android.webkit.WebResourceRequest; | import android.webkit.WebResourceRequest; | ||||
| @@ -9,6 +10,7 @@ import android.webkit.WebView; | |||||
| import android.webkit.WebViewClient; | import android.webkit.WebViewClient; | ||||
| import android.widget.EditText; | import android.widget.EditText; | ||||
| import androidx.annotation.Nullable; | |||||
| import androidx.appcompat.app.AlertDialog; | import androidx.appcompat.app.AlertDialog; | ||||
| import androidx.appcompat.app.AppCompatActivity; | import androidx.appcompat.app.AppCompatActivity; | ||||
| @@ -23,6 +25,8 @@ public class FullscreenActivity extends AppCompatActivity { | |||||
| private SensorManagerHelper sensorManagerHelper; | private SensorManagerHelper sensorManagerHelper; | ||||
| private BaseWebChromeClient chromeClient; | |||||
| @SuppressLint("SetJavaScriptEnabled") | @SuppressLint("SetJavaScriptEnabled") | ||||
| @Override | @Override | ||||
| protected void onCreate(Bundle savedInstanceState) { | protected void onCreate(Bundle savedInstanceState) { | ||||
| @@ -33,10 +37,12 @@ public class FullscreenActivity extends AppCompatActivity { | |||||
| WebSettings settings = mWebView.getSettings(); | WebSettings settings = mWebView.getSettings(); | ||||
| settings.setJavaScriptEnabled(true); | settings.setJavaScriptEnabled(true); | ||||
| mWebView.setWebViewClient(new WebViewClient(){ | |||||
| this.setWebSettings(mWebView); | |||||
| mWebView.setWebViewClient(new WebViewClient() { | |||||
| @Override | @Override | ||||
| public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { | public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { | ||||
| return false; | |||||
| return false; | |||||
| } | } | ||||
| }); | }); | ||||
| EditText textView = new EditText(this); | EditText textView = new EditText(this); | ||||
| @@ -44,10 +50,12 @@ public class FullscreenActivity extends AppCompatActivity { | |||||
| AlertDialog alertDialog = new AlertDialog.Builder(this) | AlertDialog alertDialog = new AlertDialog.Builder(this) | ||||
| .setView(textView) | .setView(textView) | ||||
| .setTitle("修改地址") | .setTitle("修改地址") | ||||
| .setPositiveButton("确定", (dialogInterface, i) -> mWebView.loadUrl("http://"+textView.getText().toString())) | |||||
| .setPositiveButton("确定", (dialogInterface, i) -> mWebView.loadUrl("http://" + textView.getText().toString())) | |||||
| .create(); | .create(); | ||||
| mWebView.loadUrl("http://116.255.135.38:86/authenticRight/login"); | |||||
| mWebView.loadUrl("http://116.255.135.38:89/homestead/login"); | |||||
| chromeClient = new BaseWebChromeClient(this); | |||||
| mWebView.setWebChromeClient(chromeClient); | |||||
| sensorManagerHelper = new SensorManagerHelper(this); | sensorManagerHelper = new SensorManagerHelper(this); | ||||
| sensorManagerHelper.setOnShakeListener(alertDialog::show); | sensorManagerHelper.setOnShakeListener(alertDialog::show); | ||||
| @@ -55,10 +63,39 @@ public class FullscreenActivity extends AppCompatActivity { | |||||
| @Override | @Override | ||||
| public boolean onKeyDown(int keyCode, KeyEvent event) { | public boolean onKeyDown(int keyCode, KeyEvent event) { | ||||
| if(keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()){ | |||||
| if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) { | |||||
| mWebView.goBack(); | mWebView.goBack(); | ||||
| return true; | return true; | ||||
| } | } | ||||
| return super.onKeyDown(keyCode, event); | return super.onKeyDown(keyCode, event); | ||||
| } | } | ||||
| public void setWebSettings(WebView mView) { | |||||
| WebSettings setting = mView.getSettings(); | |||||
| //支持Js | |||||
| setting.setJavaScriptEnabled(true); | |||||
| setting.setJavaScriptCanOpenWindowsAutomatically(true); | |||||
| //缓存模式 | |||||
| setting.setCacheMode(WebSettings.LOAD_DEFAULT); | |||||
| //支持内容重新布局 | |||||
| setting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); | |||||
| //将图片调整到适合webview的大小 | |||||
| setting.setUseWideViewPort(true); | |||||
| setting.setLoadWithOverviewMode(true); | |||||
| //设置可以访问文件 | |||||
| setting.setAllowFileAccess(true); | |||||
| //支持自动加载图片 | |||||
| setting.setLoadsImagesAutomatically(true); | |||||
| try { | |||||
| setting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); | |||||
| } catch (NoSuchMethodError e) { | |||||
| e.printStackTrace(); | |||||
| } | |||||
| } | |||||
| @Override | |||||
| protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | |||||
| chromeClient.onActivityResult(requestCode, resultCode, data); | |||||
| super.onActivityResult(requestCode, resultCode, data); | |||||
| } | |||||
| } | } | ||||
| @@ -1,5 +1,5 @@ | |||||
| <resources> | <resources> | ||||
| <string name="app_name">抽样调查</string> | |||||
| <string name="app_name">两清三化</string> | |||||
| <string name="dummy_button">Dummy Button</string> | <string name="dummy_button">Dummy Button</string> | ||||
| <string name="dummy_content">DUMMY\nCONTENT</string> | <string name="dummy_content">DUMMY\nCONTENT</string> | ||||
| </resources> | </resources> | ||||