drew 6 vuotta sitten
vanhempi
commit
046e6ca3ee

+ 54 - 0
app/src/main/java/com/ht/gate/NetCheckUtil.java

@@ -0,0 +1,54 @@
+package com.ht.gate;
+
+import android.content.Context;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+
+public class NetCheckUtil {
+
+    public static boolean checkNet(Context context) {
+        // 判断是否具有可以用于通信渠道
+        boolean mobileConnection = isMobileConnection(context);
+        boolean wifiConnection = isWIFIConnection(context);
+        if (mobileConnection == false && wifiConnection == false) {
+            // 没有网络
+            return false;
+        }
+        return true;
+    }
+
+
+    /**
+     * 判断手机接入点(APN)是否处于可以使用的状态
+     *
+     * @param context
+     * @return
+     */
+    public static boolean isMobileConnection(Context context) {
+        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
+        NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
+
+        if (networkInfo != null && networkInfo.isConnected()) {
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * 判断当前wifi是否是处于可以使用状态
+     *
+     * @param context
+     * @return
+     */
+    public static boolean isWIFIConnection(Context context) {
+        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
+        NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
+
+        if (networkInfo != null && networkInfo.isConnected()) {
+            return true;
+        }
+        return false;
+    }
+
+
+}

+ 62 - 0
app/src/main/java/com/ht/gate/TimeView.java

@@ -0,0 +1,62 @@
+package com.ht.gate;
+
+import android.annotation.SuppressLint;
+import android.content.Context;
+import android.os.Handler;
+import android.os.Message;
+import android.util.AttributeSet;
+import android.widget.TextView;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+@SuppressLint("AppCompatCustomView")
+public class TimeView extends TextView {
+    private ScheduledExecutorService executorService;
+    private Handler handler = new Handler(new Handler.Callback() {
+        @Override
+        public boolean handleMessage(@NonNull Message msg) {
+            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.CHINA);
+            setText(sdf.format(new Date()));
+            return true;
+        }
+    });
+
+    public TimeView(Context context) {
+        super(context);
+        initTimer();
+    }
+
+    public TimeView(Context context, @Nullable AttributeSet attrs) {
+        super(context, attrs);
+        initTimer();
+    }
+
+    public TimeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+        initTimer();
+    }
+
+    public TimeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
+        super(context, attrs, defStyleAttr, defStyleRes);
+        initTimer();
+    }
+
+    private void initTimer() {
+        executorService = Executors.newScheduledThreadPool(2);
+        executorService.scheduleAtFixedRate(new Runnable() {
+            @Override
+            public void run() {
+                handler.sendEmptyMessage(0);
+            }
+        }, 0, 1000, TimeUnit.MILLISECONDS);
+    }
+
+}

+ 0 - 2
app/src/main/res/layout/activity_welcome.xml

@@ -244,8 +244,6 @@
                     android:textSize="15sp" />
             </RelativeLayout>
         </LinearLayout>
-
-
     </androidx.constraintlayout.widget.ConstraintLayout>
 
     <RelativeLayout