Kaynağa Gözat

初始化界面增加webview类型选择,默认使用系统原生webview,兼容不同Android设备

weizhengliang 3 yıl önce
ebeveyn
işleme
860710f240

+ 1 - 1
app/build.gradle

@@ -8,7 +8,7 @@ android {
         minSdkVersion 15
         targetSdkVersion 30
         versionCode 1
-        versionName "1.53"
+        versionName "1.54"
         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
     }
     buildTypes {

+ 2 - 2
app/src/main/AndroidManifest.xml

@@ -38,8 +38,8 @@
                 <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
         </activity>
-        <activity android:name=".XwalkMainActivity">
-        </activity>
+        <activity android:name=".XwalkMainActivity"/>
+        <activity android:name=".MainActivity"/>
     </application>
 
 </manifest>

+ 65 - 1
app/src/main/java/com/example/informationkanban/InitActivity.java

@@ -2,8 +2,11 @@ package com.example.informationkanban;
 
 import android.content.Intent;
 import android.os.Bundle;
+import android.os.CountDownTimer;
 import android.text.TextUtils;
 import android.util.Log;
+import android.widget.RadioButton;
+import android.widget.RadioGroup;
 import android.widget.TextView;
 import android.widget.Toast;
 
@@ -11,14 +14,21 @@ import androidx.appcompat.app.AppCompatActivity;
 
 import com.blankj.utilcode.util.NetworkUtils;
 import com.example.informationkanban.utils.GetInformationUtils;
+import com.example.informationkanban.utils.NetFunctionConfig;
 
 public class InitActivity extends AppCompatActivity {
 
     private TextView tvMac;
     private TextView tvIP;
     private TextView tvVersion;
+    private TextView tvTime;
+    private RadioGroup group;
+    private RadioButton btnWebview;
+    private RadioButton btnXwalk;
     private boolean loop = true;
 
+    private CountDownTimer countDownTimer;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -28,8 +38,51 @@ public class InitActivity extends AppCompatActivity {
         tvIP = findViewById(R.id.textView_ip);
         tvVersion = findViewById(R.id.textView_version);
         tvVersion.setText("版本: V" + BuildConfig.VERSION_NAME);
+        tvTime = findViewById(R.id.textView_countdown);
+        group = findViewById(R.id.group_webview_type);
+        btnWebview = findViewById(R.id.radio_webview);
+        btnXwalk = findViewById(R.id.radio_xwalk);
+
+        //webview类型,默认为原生webview
+        if (NetFunctionConfig.getWebviewType() == 1) {
+            btnWebview.setChecked(true);
+        } else {
+            btnXwalk.setChecked(true);
+        }
+        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
+            @Override
+            public void onCheckedChanged(RadioGroup group, int checkedId) {
+                if (checkedId == R.id.radio_webview) {
+                    NetFunctionConfig.setWebviewType(1);
+                } else {
+                    NetFunctionConfig.setWebviewType(2);
+                }
+            }
+        });
+
         updateView();
 
+        initCountDownTimer();
+
+    }
+
+    private void initCountDownTimer() {
+        countDownTimer = new CountDownTimer(30000, 1000) {
+            @Override
+            public void onTick(long millisUntilFinished) {
+                long untilTime = millisUntilFinished/1000;
+                tvTime.setText("倒计时: " + untilTime + "秒");
+            }
+
+            @Override
+            public void onFinish() {
+                checkNetwork();
+            }
+        };
+        countDownTimer.start();
+    }
+
+    private void checkNetwork() {
         new Thread(new Runnable() {
             @Override
             public void run() {
@@ -66,10 +119,21 @@ public class InitActivity extends AppCompatActivity {
         runOnUiThread(new Runnable() {
             @Override
             public void run() {
-                Intent intent = new Intent(InitActivity.this, XwalkMainActivity.class);
+                Intent intent = new Intent();
+                if (NetFunctionConfig.getWebviewType() == 1) {
+                    intent.setClass(getApplicationContext(), MainActivity.class);
+                } else {
+                    intent.setClass(getApplicationContext(), XwalkMainActivity.class);
+                }
                 startActivity(intent);
                 finish();
             }
         });
     }
+
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+        countDownTimer.cancel();
+    }
 }

+ 10 - 0
app/src/main/java/com/example/informationkanban/utils/NetFunctionConfig.java

@@ -10,6 +10,16 @@ public class NetFunctionConfig {
 
     private static final String GIT_TIME = "GIT_TIME";
 
+    private static final String WEBVIEW_TYPE = "WEBVIEW_TYPE";
+
+    public static void setWebviewType(int type) {
+        getEditor().putInt(WEBVIEW_TYPE, type).apply();
+    }
+
+    public static int getWebviewType() {
+        return getSP().getInt(WEBVIEW_TYPE, 1);
+    }
+
     public static void setTime(String time) {
         getEditor().putString(GIT_TIME, time).apply();
     }

+ 42 - 4
app/src/main/res/layout/init_layout.xml

@@ -1,14 +1,16 @@
 <?xml version="1.0" encoding="utf-8"?>
-<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
-    xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
+    android:layout_height="match_parent"
+    android:orientation="vertical"
+    android:gravity="center_horizontal">
 
     <TextView
         android:id="@+id/textView_mac"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
+        android:layout_marginTop="40dp"
         android:text="mac地址"
         android:textSize="28sp"
         app:layout_constraintBottom_toTopOf="@+id/textView_ip"
@@ -20,6 +22,7 @@
         android:id="@+id/textView_ip"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
+        android:layout_marginTop="40dp"
         android:text="IP地址"
         android:textSize="28sp"
         app:layout_constraintBottom_toBottomOf="parent"
@@ -27,14 +30,49 @@
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent" />
 
+    <RadioGroup
+        android:id="@+id/group_webview_type"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="40dp"
+        android:orientation="horizontal">
+        <RadioButton
+            android:id="@+id/radio_webview"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="使用原生Webview"
+            android:textSize="20sp"/>
+        <RadioButton
+            android:id="@+id/radio_xwalk"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginLeft="20dp"
+            android:layout_marginStart="20dp"
+            android:text="使用内置Xwalk"
+            android:textSize="20sp"/>
+    </RadioGroup>
+
+    <TextView
+        android:id="@+id/textView_countdown"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="40dp"
+        android:text="倒计时"
+        android:textSize="28sp"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+
     <TextView
         android:id="@+id/textView_version"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
+        android:layout_marginTop="80dp"
         android:text="APP版本"
         android:textSize="28sp"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toBottomOf="@+id/textView_ip" />
-</androidx.constraintlayout.widget.ConstraintLayout>
+</LinearLayout>