Explorar o código

1.增加网络监听逻辑
2.增加无网络提示框以及UI

FailedToRead %!s(int64=4) %!d(string=hai) anos
pai
achega
0c3232d345

+ 4 - 0
app/src/main/AndroidManifest.xml

@@ -49,6 +49,10 @@
             </intent-filter>
 
         </activity>
+        <activity
+            android:name=".activity.DialogNetworkActivity"
+            android:screenOrientation="landscape"
+            android:theme="@style/dialog"></activity>
     </application>
 
 

+ 98 - 0
app/src/main/java/com/edufound/ott/zijie/yuwen/activity/DialogNetworkActivity.java

@@ -0,0 +1,98 @@
+package com.edufound.ott.zijie.yuwen.activity;
+
+import android.app.Activity;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.graphics.Color;
+import android.os.Bundle;
+import android.view.KeyEvent;
+import android.view.MotionEvent;
+import android.view.View;
+import android.widget.FrameLayout;
+import android.widget.TextView;
+
+import androidx.annotation.Nullable;
+
+import com.edufound.ott.zijie.yuwen.R;
+import com.edufound.ott.zijie.yuwen.application.MyApplication;
+import com.edufound.ott.zijie.yuwen.util.ContextUtil;
+
+public class DialogNetworkActivity extends Activity {
+
+    FrameLayout btnCancel;
+    FrameLayout btnConfirm;
+
+    @Override
+    protected void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        ContextUtil.setDialogNetWorkonShow(true);
+        View bv = this.findViewById(android.R.id.title);
+        bv.setVisibility(View.GONE);
+        setFinishOnTouchOutside(false);
+        setContentView(R.layout.dialog_error);
+        init();
+        initCloseMain();
+    }
+
+    void init() {
+//        btnCancel = findViewById(R.id.dialog_cancel_button);
+        btnConfirm = findViewById(R.id.dialog_confirm_button);
+//        btnCancel.setOnTouchListener(buttonTouch);
+        btnConfirm.setOnTouchListener(buttonTouch);
+    }
+
+    View.OnTouchListener buttonTouch = new View.OnTouchListener() {
+        @Override
+        public boolean onTouch(View view, MotionEvent motionEvent) {
+            switch (motionEvent.getAction()) {
+                case MotionEvent.ACTION_DOWN:
+                case MotionEvent.ACTION_MOVE:
+                case MotionEvent.ACTION_HOVER_MOVE:
+                    view.setBackground(null);
+                    view.setBackgroundResource(R.drawable.dialog_error_button_touch_bg);
+                    ((TextView) ((FrameLayout) view).getChildAt(0)).setAlpha(1f);
+                    ((TextView) ((FrameLayout) view).getChildAt(0)).setTextColor(Color.WHITE);
+                    break;
+                case MotionEvent.ACTION_CANCEL:
+//                    view.setBackground(null);
+//                    view.setBackgroundResource(R.drawable.dialog_error_button_bg);
+//                    ((TextView) ((FrameLayout) view).getChildAt(0)).setAlpha(0.6f);
+//                    ((TextView) ((FrameLayout) view).getChildAt(0)).setTextColor(Color.BLACK);
+                    break;
+                case MotionEvent.ACTION_UP:
+                    MyApplication.exitApp();
+                    break;
+            }
+            return true;
+        }
+    };
+
+    @Override
+    public boolean onKeyDown(int keyCode, KeyEvent event) {
+        return true;
+    }
+
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+        unregisterReceiver(receiver);
+        ContextUtil.setDialogNetWorkonShow(false);
+    }
+
+    void initCloseMain() {
+        IntentFilter intentFilter = new IntentFilter();
+        intentFilter.addAction(ContextUtil.CLOSE_DIALOG_NETWORK);
+        registerReceiver(receiver, intentFilter);
+    }
+
+
+    private BroadcastReceiver receiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            finish();
+        }
+    };
+
+}

+ 21 - 0
app/src/main/java/com/edufound/ott/zijie/yuwen/activity/MainActivity.java

@@ -3,6 +3,7 @@ package com.edufound.ott.zijie.yuwen.activity;
 import android.animation.ObjectAnimator;
 import android.app.Activity;
 import android.content.Context;
+import android.content.Intent;
 import android.graphics.Color;
 import android.os.Bundle;
 import android.view.View;
@@ -10,6 +11,7 @@ import android.view.ViewGroup;
 import android.view.animation.Interpolator;
 import android.view.animation.LinearInterpolator;
 import android.webkit.WebView;
+import android.widget.Button;
 import android.widget.FrameLayout;
 import android.widget.ImageView;
 
@@ -54,6 +56,18 @@ public class MainActivity extends BaseActivity<MainModel, MainView, MainPersente
         mPresenter.initWeb();
         mPresenter.openWeb(getIntent());
         mMainFrame.addView(getWebView());
+        ///test
+        Button ben = new Button(mActivity);
+        ben.setLayoutParams(new FrameLayout.LayoutParams(100, 100));
+        ben.setText("click show dialog error page");
+        ben.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View view) {
+                Intent intent = new Intent(MainActivity.this, DialogNetworkActivity.class);
+                startActivity(intent);
+            }
+        });
+        mMainFrame.addView(ben);
     }
 
 
@@ -115,4 +129,11 @@ public class MainActivity extends BaseActivity<MainModel, MainView, MainPersente
     public WebView getWebView() {
         return mWebView;
     }
+
+    @Override
+    public void showDialogNetWork() {
+        Intent intent = new Intent(mActivity, DialogNetworkActivity.class);
+        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        mActivity.startActivity(intent);
+    }
 }

+ 41 - 0
app/src/main/java/com/edufound/ott/zijie/yuwen/application/MyApplication.java

@@ -1,13 +1,21 @@
 package com.edufound.ott.zijie.yuwen.application;
 
+import android.app.Activity;
 import android.app.Application;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.os.Process;
 
 import com.edufound.ott.zijie.yuwen.receiver.HomeKeyEventReceiver;
 import com.edufound.ott.zijie.yuwen.receiver.NetworkChangeReceiver;
 import com.edufound.ott.zijie.yuwen.util.ContextUtil;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
 public class MyApplication extends Application {
     //网络异常监听
     NetworkChangeReceiver mNetworkReceiver;
@@ -30,4 +38,37 @@ public class MyApplication extends Application {
         netFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
         registerReceiver(mNetworkReceiver, netFilter);
     }
+
+    private static List<Activity> getAllActivitys() {
+        List<Activity> list = new ArrayList<>();
+        try {
+            Class<?> activityThread = Class.forName("android.app.ActivityThread");
+            Method currentActivityThread = activityThread.getDeclaredMethod("currentActivityThread");
+            currentActivityThread.setAccessible(true);
+            //获取主线程对象
+            Object activityThreadObject = currentActivityThread.invoke(null);
+            Field mActivitiesField = activityThread.getDeclaredField("mActivities");
+            mActivitiesField.setAccessible(true);
+            Map<Object, Object> mActivities = (Map<Object, Object>) mActivitiesField.get(activityThreadObject);
+            for (Map.Entry<Object, Object> entry : mActivities.entrySet()) {
+                Object value = entry.getValue();
+                Class<?> activityClientRecordClass = value.getClass();
+                Field activityField = activityClientRecordClass.getDeclaredField("activity");
+                activityField.setAccessible(true);
+                Object o = activityField.get(value);
+                list.add((Activity) o);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return list;
+    }
+
+    public static void exitApp() {
+        List<Activity> myAclist = getAllActivitys();
+        for (int i = 0; i < myAclist.size(); i++) {
+            myAclist.get(i).finish();
+        }
+        android.os.Process.killProcess(Process.myPid());
+    }
 }

+ 13 - 11
app/src/main/java/com/edufound/ott/zijie/yuwen/mvp/presenter/MainPersenter.java

@@ -94,23 +94,25 @@ public class MainPersenter extends BasePresenter<MainModel, MainView> {
             @Override
             public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                 super.onReceivedError(view, errorCode, description, failingUrl);
-                view.loadUrl("");
+                view.loadUrl("");// 避免出现默认的错误界面
 //                showDisConnNeWorkWindow();
+                Logger.e("onReceivedError--4参数");
+                getView().showDialogNetWork();
             }
 
             @Override
             public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
                 super.onReceivedHttpError(view, request, errorResponse);
-//                int statusCode = 0;
-//                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
-//                    statusCode = errorResponse.getStatusCode();
-//                }
-//                System.out.println("onReceivedHttpError code = " + statusCode);
-//                if (404 == statusCode) {
-//                    view.loadUrl("about:blank");// 避免出现默认的错误界面
-//                    showDisConnNeWorkWindow();
-//                }
-
+                int statusCode = 0;
+                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
+                    statusCode = errorResponse.getStatusCode();
+                }
+                System.out.println("onReceivedHttpError code = " + statusCode);
+                if (404 == statusCode) {
+                    view.loadUrl("");// 避免出现默认的错误界面
+                }
+                Logger.e("onReceivedError--3参数");
+                getView().showDialogNetWork();
             }
 
             @Override

+ 2 - 0
app/src/main/java/com/edufound/ott/zijie/yuwen/mvp/view/MainView.java

@@ -12,4 +12,6 @@ public interface MainView extends IView {
     void loadUrl(String url);
 
     WebView getWebView();
+
+    void showDialogNetWork();
 }

+ 18 - 3
app/src/main/java/com/edufound/ott/zijie/yuwen/receiver/NetworkChangeReceiver.java

@@ -9,6 +9,8 @@ import android.os.Handler;
 import android.os.Message;
 import android.telephony.TelephonyManager;
 
+import com.edufound.ott.zijie.yuwen.activity.DialogNetworkActivity;
+import com.edufound.ott.zijie.yuwen.util.ContextUtil;
 import com.edufound.ott.zijie.yuwen.util.Logger;
 
 
@@ -20,6 +22,8 @@ public class NetworkChangeReceiver extends BroadcastReceiver {
     final int DISCONNECT = 0x21;
     final int CONNECT = 0x22;
     String strNetworkType;
+    Context mContext;
+    Intent startIntent;
 
     public NetworkChangeReceiver() {
 
@@ -32,6 +36,7 @@ public class NetworkChangeReceiver extends BroadcastReceiver {
     }
 
     public void GetNetworkType(Context context) {
+        mContext = context;
         ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
         NetworkInfo networkInfo = connectMgr.getActiveNetworkInfo();
         if (networkInfo != null && networkInfo.isConnected()) {
@@ -83,11 +88,21 @@ public class NetworkChangeReceiver extends BroadcastReceiver {
         public boolean handleMessage(Message msg) {
             switch (msg.what) {
                 case DISCONNECT:
-//                    mView.DisconnNetWork();
+                    //无网络连接
+                    Logger.e("NetworkChangeReceiver--DISCONNECT");
+                    if (ContextUtil.isDialogNetWorkonShow()) {
+                        //如果已经展示了就不展示了
+                        return true;
+                    }
+                    startIntent = new Intent(mContext, DialogNetworkActivity.class);
+                    startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                    mContext.startActivity(startIntent);
                     break;
                 case CONNECT:
-                    handler.removeMessages(DISCONNECT);
-//                    mView.ConnNeWork();
+                    //有网络连接
+                    Logger.e("NetworkChangeReceiver--CONNECT");
+                    Intent intent_restart = new Intent(ContextUtil.CLOSE_DIALOG_NETWORK);
+                    mContext.sendBroadcast(intent_restart);
                     break;
             }
             return false;

+ 11 - 0
app/src/main/java/com/edufound/ott/zijie/yuwen/util/ContextUtil.java

@@ -6,6 +6,9 @@ public class ContextUtil {
     private static Long APP_START_TIME;
     private static Context mApplicationContext;
     private static String Uuid;
+    public static String CLOSE_DIALOG_NETWORK = "com.edufound.ott.zijie.close.network";
+
+    public static boolean DialogNetWorkonShow = true;
 
     public static Long getAppStartTime() {
         return APP_START_TIME;
@@ -31,5 +34,13 @@ public class ContextUtil {
         Uuid = uuid;
     }
 
+    public static boolean isDialogNetWorkonShow() {
+        return DialogNetWorkonShow;
+    }
+
+    public static void setDialogNetWorkonShow(boolean dialogNetWorkonShow) {
+        DialogNetWorkonShow = dialogNetWorkonShow;
+    }
+
 
 }

BIN=BIN
app/src/main/res/drawable-hdpi/dialog_icon.png


BIN=BIN
app/src/main/res/drawable-ldpi/dialog_icon.png


BIN=BIN
app/src/main/res/drawable-mdpi/dialog_icon.png


+ 12 - 0
app/src/main/res/drawable/dialog_error_bg.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+    <!-- 设置圆角半径  还可分别详细设置上下左右4个圆角-->
+    <corners android:radius="16dp" />
+    <!-- 内部颜色  -->
+    <solid android:color="@android:color/white" />
+    <!-- 边框宽度 边框颜色 -->
+    <stroke
+        android:width="1dp"
+        android:color="#14000000" />
+</shape>

+ 11 - 0
app/src/main/res/drawable/dialog_error_button_bg.xml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+    <!-- 这里是设置为四周 也可以单独设置某个位置为圆角-->
+    <corners android:radius="12dp" />
+    <solid android:color="@android:color/white" />
+    <stroke
+        android:width="0.67dp"
+        android:color="#14000000" />
+
+
+</shape>

+ 11 - 0
app/src/main/res/drawable/dialog_error_button_touch_bg.xml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+    <!-- 这里是设置为四周 也可以单独设置某个位置为圆角-->
+    <corners android:radius="12dp" />
+    <solid android:color="#5ecaff" />
+    <stroke
+        android:width="0.67dp"
+        android:color="#4bc4ff" />
+
+
+</shape>

+ 107 - 0
app/src/main/res/layout/dialog_error.xml

@@ -0,0 +1,107 @@
+<?xml version="1.0" encoding="utf-8"?>
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/dialogalert"
+    android:layout_width="480dp"
+    android:layout_height="266dp"
+    android:background="@drawable/dialog_error_bg">
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:orientation="vertical">
+
+        <Space
+            android:layout_width="match_parent"
+            android:layout_height="0dp"
+            android:layout_weight="0.2"></Space>
+
+        <ImageView
+            android:layout_width="wrap_content"
+            android:layout_height="0dp"
+            android:layout_gravity="center"
+            android:layout_weight="1"
+            android:src="@drawable/dialog_icon"></ImageView>
+
+
+        <FrameLayout
+            android:layout_width="match_parent"
+            android:layout_height="0dp"
+            android:layout_weight="0.4">
+
+            <TextView
+                android:layout_width="match_parent"
+                android:layout_height="match_parent"
+                android:layout_gravity="center"
+                android:gravity="center"
+                android:text="阿欧,请检查你的网络,一会再来找他吧!"
+                android:textColor="#99000000"
+                android:textSize="15sp"></TextView>
+        </FrameLayout>
+
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="0dp"
+            android:layout_gravity="center"
+            android:layout_weight="0.8"
+            android:gravity="center"
+            android:orientation="horizontal">
+
+            <Space
+                android:layout_width="0dp"
+                android:layout_height="match_parent"
+                android:layout_weight="0.1"></Space>
+
+            <FrameLayout
+                android:id="@+id/dialog_cancel_button"
+                android:layout_width="0dp"
+                android:layout_height="42dp"
+                android:layout_weight="1.2"
+                android:background="@drawable/dialog_error_button_bg"
+                android:visibility="gone">
+
+                <TextView
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_gravity="center"
+                    android:alpha="0.6"
+                    android:gravity="center"
+                    android:text="取消"
+                    android:textColor="@color/black"
+                    android:textSize="14sp"
+                    android:textStyle="bold"></TextView>
+            </FrameLayout>
+
+            <Space
+                android:layout_width="0dp"
+                android:layout_height="match_parent"
+                android:layout_weight="0.05"
+                android:visibility="gone"></Space>
+
+            <FrameLayout
+                android:id="@+id/dialog_confirm_button"
+                android:layout_width="0dp"
+                android:layout_height="42dp"
+                android:layout_weight="1.2"
+                android:background="@drawable/dialog_error_button_bg">
+
+                <TextView
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_gravity="center"
+                    android:alpha="0.6"
+                    android:gravity="center"
+                    android:text="确认"
+                    android:textColor="@color/black"
+                    android:textSize="14sp"
+                    android:textStyle="bold"></TextView>
+            </FrameLayout>
+
+            <Space
+                android:layout_width="0dp"
+                android:layout_height="match_parent"
+                android:layout_weight="0.1"></Space>
+        </LinearLayout>
+    </LinearLayout>
+
+
+</FrameLayout>

+ 2 - 0
app/src/main/res/values/colors.xml

@@ -1,6 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
+    <color name="transparent">#00000000</color><!--透明色 -->
     <color name="colorPrimary">#6200EE</color>
     <color name="colorPrimaryDark">#3700B3</color>
     <color name="colorAccent">#03DAC5</color>
+    <color name="black">#000000</color>
 </resources>

+ 9 - 0
app/src/main/res/values/styles.xml

@@ -7,4 +7,13 @@
         <item name="colorAccent">@color/colorAccent</item>
     </style>
 
+    <style name="dialog" parent="@android:style/Theme.Dialog">
+        <item name="android:windowFrame">@null</item>
+        <item name="android:windowIsFloating">true</item>
+        <item name="android:windowIsTranslucent">true</item>
+        <item name="android:windowNoTitle">false</item>
+        <item name="android:background">@color/transparent</item>
+        <item name="android:windowBackground">@color/transparent</item>
+        <item name="android:backgroundDimEnabled">true</item>
+    </style>
 </resources>