MainActivity.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package com.example.informationkanban;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.os.Looper;
  6. import android.text.TextUtils;
  7. import android.util.Log;
  8. import android.view.KeyEvent;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.view.Window;
  12. import android.view.WindowManager;
  13. import android.webkit.WebChromeClient;
  14. import android.webkit.WebSettings;
  15. import android.webkit.WebView;
  16. import android.webkit.WebViewClient;
  17. import android.widget.LinearLayout;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20. import com.example.informationkanban.dialog.UrlConfigDialogHelper;
  21. import com.example.informationkanban.utils.AppUtil;
  22. import com.example.informationkanban.utils.GetInformationUtils;
  23. import com.example.informationkanban.utils.LocaleMangerUtils;
  24. import com.example.informationkanban.utils.NetFunctionConfig;
  25. import com.example.informationkanban.view.SplashView;
  26. import java.util.Random;
  27. public class MainActivity extends AppCompatActivity {
  28. private String TAG = MainActivity.class.getSimpleName();
  29. private WebView webView;
  30. private String MAC = "";
  31. private LinearLayout initView;
  32. private SplashView splashView;
  33. private long backClickTime = 0;
  34. private final Handler handler = new Handler(Looper.getMainLooper());
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. int languageId = NetFunctionConfig.getLanguageId(this);
  38. LocaleMangerUtils.setApplicationLanguageByIndex(this, languageId);
  39. super.onCreate(savedInstanceState);
  40. requestWindowFeature(Window.FEATURE_NO_TITLE);
  41. getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
  42. WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
  43. WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
  44. WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
  45. setContentView(R.layout.activity_main);
  46. init();
  47. MAC = GetInformationUtils.getIMEI(this);
  48. //如果获取id为空则随机生成
  49. if (TextUtils.isEmpty(MAC)) {
  50. if (TextUtils.isEmpty(NetFunctionConfig.getUUID())) {
  51. MAC = "8" + new Random().nextInt(10) + System.currentTimeMillis();
  52. NetFunctionConfig.setUUID(MAC);
  53. } else {
  54. MAC = NetFunctionConfig.getUUID();
  55. }
  56. }
  57. String url = NetFunctionConfig.getCBDUrl();
  58. if (BuildConfig.type.equalsIgnoreCase("s64_ldjc")) {
  59. url = NetFunctionConfig.getCBDS64Url();
  60. } else if (BuildConfig.type.equalsIgnoreCase("s65_shzy")) {
  61. url = NetFunctionConfig.getWdklS65Url();
  62. } else if (BuildConfig.type.equalsIgnoreCase("s64_wxh")) {
  63. url = NetFunctionConfig.getWxhUrl();
  64. }
  65. if (TextUtils.isEmpty(url)) {
  66. //url为空则需要手动配置
  67. UrlConfigDialogHelper.showDialog(MainActivity.this);
  68. } else {
  69. if (AppUtil.isNetConnection(MainActivity.this)) {
  70. accessTheWebpage();
  71. } else {
  72. initView.setVisibility(View.VISIBLE);
  73. //检测网络连接
  74. handler.postDelayed(this::checkNetwork, 10000);
  75. }
  76. }
  77. }
  78. private void checkNetwork() {
  79. Log.e(TAG, "start check network...");
  80. if (AppUtil.isNetConnection(MainActivity.this)) {
  81. accessTheWebpage();
  82. } else {
  83. handler.postDelayed(this::checkNetwork, 10000);
  84. }
  85. }
  86. @Override
  87. protected void onResume() {
  88. super.onResume();
  89. View decorView = getWindow().getDecorView();
  90. decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  91. | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
  92. | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  93. | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  94. | View.SYSTEM_UI_FLAG_FULLSCREEN
  95. | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
  96. }
  97. private void init() {
  98. //获得控件
  99. webView = findViewById(R.id.wv_webview);
  100. initView = findViewById(R.id.ll_splash);
  101. splashView = findViewById(R.id.splash_view);
  102. }
  103. private void accessTheWebpage() {
  104. handler.removeCallbacksAndMessages(null);
  105. Log.e(TAG, "access web page...");
  106. webView.setInitialScale(100);//设置缩放比例
  107. WebSettings settings = webView.getSettings();
  108. settings.setJavaScriptCanOpenWindowsAutomatically(true);//设置js可以直接打开窗口,如window.open(),默认为false
  109. settings.setJavaScriptEnabled(true);//是否允许执行js,默认为false。设置true时,会提醒可能造成XSS漏洞
  110. settings.setSupportZoom(true);//是否可以缩放,默认true
  111. settings.setBuiltInZoomControls(false);//是否显示缩放按钮,默认false
  112. settings.setUseWideViewPort(true);//设置此属性,可任意比例缩放。大视图模式
  113. settings.setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
  114. settings.setAppCacheEnabled(false);//是否使用缓存
  115. settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
  116. settings.setDomStorageEnabled(true);//DOM Storage
  117. settings.setMediaPlaybackRequiresUserGesture(false);
  118. //String myUrl = "192.168.1.196:8080";
  119. //String myUrl = NetFunctionConfig.getCBDUrl();
  120. String myUrl;
  121. //例如:http://cbd.wdklian.com?token=123456
  122. if (BuildConfig.type.equalsIgnoreCase("s65_shzy")) {
  123. myUrl = NetFunctionConfig.getWdklS65Url();
  124. } else if (BuildConfig.type.equalsIgnoreCase("s64_ldjc")) {
  125. myUrl = NetFunctionConfig.getCBDS64Url() + MAC;
  126. } else if (BuildConfig.type.equalsIgnoreCase("s64_wxh")) {
  127. myUrl = NetFunctionConfig.getWxhUrl();
  128. } else {
  129. //都要带token
  130. myUrl = NetFunctionConfig.getCBDUrl() + MAC;
  131. }
  132. //Toast.makeText(MainActivity.this, "fetch: " + myUrl, Toast.LENGTH_LONG).show();
  133. Toast.makeText(MainActivity.this, "正在加载数据,请稍后...", Toast.LENGTH_LONG).show();
  134. splashView.finshSplash();
  135. initView.setVisibility(View.GONE);
  136. Log.e(TAG, "load url: " + myUrl);
  137. webView.loadUrl(myUrl);
  138. //系统默认会通过手机浏览器打开网页,为了能够直接通过WebView显示网页,则必须设置
  139. webView.setWebViewClient(new WebViewClient() {
  140. @Override
  141. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  142. //使用WebView加载显示url
  143. view.loadUrl(url);
  144. //返回true
  145. return true;
  146. }
  147. /*@Override
  148. public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
  149. return true;
  150. //return super.shouldOverrideKeyEvent(view, event);
  151. }*/
  152. });
  153. webView.setWebChromeClient(new WebChromeClient() {
  154. @Override
  155. public void onProgressChanged(WebView view, int newProgress) { //显示加载进度
  156. Log.d(TAG, "load webview..." + newProgress + "%");
  157. }
  158. });
  159. }
  160. @Override
  161. public boolean onKeyDown(int keyCode, KeyEvent event) {
  162. if (keyCode == KeyEvent.KEYCODE_BACK) {
  163. if (webView != null && webView.canGoBack()) {
  164. webView.goBack();
  165. return true;
  166. }
  167. long currentTime = System.currentTimeMillis();
  168. // 3秒内连按两次后退按钮,退出应用
  169. if (currentTime - backClickTime < 3000) {
  170. finish();
  171. } else {
  172. Toast.makeText(getApplicationContext(), "再按一次返回键退出", Toast.LENGTH_SHORT).show();
  173. backClickTime = currentTime;
  174. }
  175. return true;
  176. }
  177. return super.onKeyDown(keyCode, event);
  178. }
  179. //销毁Webview
  180. @Override
  181. protected void onDestroy() {
  182. handler.removeCallbacksAndMessages(null);
  183. if (webView != null) {
  184. //webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
  185. //webView.clearHistory();
  186. ((ViewGroup) webView.getParent()).removeView(webView);
  187. webView.destroy();
  188. webView = null;
  189. }
  190. super.onDestroy();
  191. }
  192. }