123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- package com.example.informationkanban;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Looper;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.Window;
- import android.view.WindowManager;
- import android.webkit.WebChromeClient;
- import android.webkit.WebSettings;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.example.informationkanban.dialog.UrlConfigDialogHelper;
- import com.example.informationkanban.utils.AppUtil;
- import com.example.informationkanban.utils.GetInformationUtils;
- import com.example.informationkanban.utils.LocaleMangerUtils;
- import com.example.informationkanban.utils.NetFunctionConfig;
- import com.example.informationkanban.view.SplashView;
- import java.util.Random;
- public class MainActivity extends AppCompatActivity {
- private String TAG = MainActivity.class.getSimpleName();
- private WebView webView;
- private String MAC = "";
- private LinearLayout initView;
- private SplashView splashView;
- private long backClickTime = 0;
- private final Handler handler = new Handler(Looper.getMainLooper());
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- int languageId = NetFunctionConfig.getLanguageId(this);
- LocaleMangerUtils.setApplicationLanguageByIndex(this, languageId);
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
- WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
- WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
- WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
- setContentView(R.layout.activity_main);
- init();
- MAC = GetInformationUtils.getIMEI(this);
- //如果获取id为空则随机生成
- if (TextUtils.isEmpty(MAC)) {
- if (TextUtils.isEmpty(NetFunctionConfig.getUUID())) {
- MAC = "8" + new Random().nextInt(10) + System.currentTimeMillis();
- NetFunctionConfig.setUUID(MAC);
- } else {
- MAC = NetFunctionConfig.getUUID();
- }
- }
- String url = NetFunctionConfig.getCBDUrl();
- if (BuildConfig.type.equalsIgnoreCase("s64_ldjc")) {
- url = NetFunctionConfig.getCBDS64Url();
- } else if (BuildConfig.type.equalsIgnoreCase("s65_shzy")) {
- url = NetFunctionConfig.getWdklS65Url();
- } else if (BuildConfig.type.equalsIgnoreCase("s64_wxh")) {
- url = NetFunctionConfig.getWxhUrl();
- }
- if (TextUtils.isEmpty(url)) {
- //url为空则需要手动配置
- UrlConfigDialogHelper.showDialog(MainActivity.this);
- } else {
- if (AppUtil.isNetConnection(MainActivity.this)) {
- accessTheWebpage();
- } else {
- initView.setVisibility(View.VISIBLE);
- //检测网络连接
- handler.postDelayed(this::checkNetwork, 10000);
- }
- }
- }
- private void checkNetwork() {
- Log.e(TAG, "start check network...");
- if (AppUtil.isNetConnection(MainActivity.this)) {
- accessTheWebpage();
- } else {
- handler.postDelayed(this::checkNetwork, 10000);
- }
- }
- @Override
- protected void onResume() {
- super.onResume();
- View decorView = getWindow().getDecorView();
- decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
- | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
- | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
- | View.SYSTEM_UI_FLAG_FULLSCREEN
- | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
- }
- private void init() {
- //获得控件
- webView = findViewById(R.id.wv_webview);
- initView = findViewById(R.id.ll_splash);
- splashView = findViewById(R.id.splash_view);
- }
- private void accessTheWebpage() {
- handler.removeCallbacksAndMessages(null);
- Log.e(TAG, "access web page...");
- webView.setInitialScale(100);//设置缩放比例
- WebSettings settings = webView.getSettings();
- settings.setJavaScriptCanOpenWindowsAutomatically(true);//设置js可以直接打开窗口,如window.open(),默认为false
- settings.setJavaScriptEnabled(true);//是否允许执行js,默认为false。设置true时,会提醒可能造成XSS漏洞
- settings.setSupportZoom(true);//是否可以缩放,默认true
- settings.setBuiltInZoomControls(false);//是否显示缩放按钮,默认false
- settings.setUseWideViewPort(true);//设置此属性,可任意比例缩放。大视图模式
- settings.setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
- settings.setAppCacheEnabled(false);//是否使用缓存
- settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
- settings.setDomStorageEnabled(true);//DOM Storage
- settings.setMediaPlaybackRequiresUserGesture(false);
- //String myUrl = "192.168.1.196:8080";
- //String myUrl = NetFunctionConfig.getCBDUrl();
- String myUrl;
- //例如:http://cbd.wdklian.com?token=123456
- if (BuildConfig.type.equalsIgnoreCase("s65_shzy")) {
- myUrl = NetFunctionConfig.getWdklS65Url();
- } else if (BuildConfig.type.equalsIgnoreCase("s64_ldjc")) {
- myUrl = NetFunctionConfig.getCBDS64Url() + MAC;
- } else if (BuildConfig.type.equalsIgnoreCase("s64_wxh")) {
- myUrl = NetFunctionConfig.getWxhUrl();
- } else {
- //都要带token
- myUrl = NetFunctionConfig.getCBDUrl() + MAC;
- }
- //Toast.makeText(MainActivity.this, "fetch: " + myUrl, Toast.LENGTH_LONG).show();
- Toast.makeText(MainActivity.this, "正在加载数据,请稍后...", Toast.LENGTH_LONG).show();
- splashView.finshSplash();
- initView.setVisibility(View.GONE);
- Log.e(TAG, "load url: " + myUrl);
- webView.loadUrl(myUrl);
- //系统默认会通过手机浏览器打开网页,为了能够直接通过WebView显示网页,则必须设置
- webView.setWebViewClient(new WebViewClient() {
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- //使用WebView加载显示url
- view.loadUrl(url);
- //返回true
- return true;
- }
- /*@Override
- public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
- return true;
- //return super.shouldOverrideKeyEvent(view, event);
- }*/
- });
- webView.setWebChromeClient(new WebChromeClient() {
- @Override
- public void onProgressChanged(WebView view, int newProgress) { //显示加载进度
- Log.d(TAG, "load webview..." + newProgress + "%");
- }
- });
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- if (webView != null && webView.canGoBack()) {
- webView.goBack();
- return true;
- }
- long currentTime = System.currentTimeMillis();
- // 3秒内连按两次后退按钮,退出应用
- if (currentTime - backClickTime < 3000) {
- finish();
- } else {
- Toast.makeText(getApplicationContext(), "再按一次返回键退出", Toast.LENGTH_SHORT).show();
- backClickTime = currentTime;
- }
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
- //销毁Webview
- @Override
- protected void onDestroy() {
- handler.removeCallbacksAndMessages(null);
- if (webView != null) {
- //webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
- //webView.clearHistory();
- ((ViewGroup) webView.getParent()).removeView(webView);
- webView.destroy();
- webView = null;
- }
- super.onDestroy();
- }
- }
|