Prechádzať zdrojové kódy

1.增加home监听
2.优化代码

FailedToRead 4 rokov pred
rodič
commit
c6eaf3af89

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

@@ -27,11 +27,10 @@
         android:roundIcon="@mipmap/ic_launcher_round"
         android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
         android:usesCleartextTraffic="true">
-
         <activity
             android:name=".activity.MainActivity"
             android:screenOrientation="landscape"
-            android:theme="@style/AppStartTheme">
+            android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
 

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

@@ -1,14 +1,18 @@
 package com.edufound.ott.zijie.yuwen.application;
 
 import android.app.Application;
+import android.content.Intent;
 import android.content.IntentFilter;
 
+import com.edufound.ott.zijie.yuwen.receiver.HomeKeyEventReceiver;
 import com.edufound.ott.zijie.yuwen.receiver.NetworkChangeReceiver;
 import com.edufound.ott.zijie.yuwen.util.ContextUtil;
 
 public class MyApplication extends Application {
     //网络异常监听
     NetworkChangeReceiver mNetworkReceiver;
+    //按home键的receiver
+    HomeKeyEventReceiver mHomeReceiver;
 
     @Override
     public void onCreate() {
@@ -19,6 +23,8 @@ public class MyApplication extends Application {
     }
 
     void initBoardCastReceiver() {
+        mHomeReceiver = new HomeKeyEventReceiver();
+        registerReceiver(mHomeReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
         mNetworkReceiver = new NetworkChangeReceiver();
         IntentFilter netFilter = new IntentFilter();
         netFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");

+ 0 - 13
app/src/main/java/com/edufound/ott/zijie/yuwen/mvp/model/MainModel.java

@@ -40,16 +40,6 @@ public class MainModel implements MainModelInter {
         return params.toString();
     }
 
-    @Override
-    public void setWebBackGroundColor(int color) {
-
-    }
-
-    @JavascriptInterface
-    @Override
-    public void closeApp() {
-        android.os.Process.killProcess(Process.myPid());
-    }
 }
 
 
@@ -58,7 +48,4 @@ interface MainModelInter extends IModel {
 
     String getLoadParams(Activity activity);
 
-    void setWebBackGroundColor(int color);
-
-    void closeApp();
 }

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

@@ -5,7 +5,9 @@ import android.content.Intent;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Color;
+import android.net.Uri;
 import android.os.Build;
+import android.os.Process;
 import android.view.View;
 import android.webkit.JavascriptInterface;
 import android.webkit.JsResult;
@@ -19,13 +21,14 @@ import android.webkit.WebViewClient;
 import com.edufound.ott.zijie.yuwen.base.BasePresenter;
 import com.edufound.ott.zijie.yuwen.mvp.model.MainModel;
 import com.edufound.ott.zijie.yuwen.mvp.view.MainView;
+import com.edufound.ott.zijie.yuwen.util.ContextUtil;
 import com.edufound.ott.zijie.yuwen.util.Logger;
+import com.edufound.ott.zijie.yuwen.util.SharedPerfenceUtil;
 
 public class MainPersenter extends BasePresenter<MainModel, MainView> {
 
     private String mLoadUrl;
 
-    @SuppressLint("JavascriptInterface")
     public void initWeb() {
         if (Build.VERSION.SDK_INT >= 19) {
             getView().getWebView().getSettings().setLoadsImagesAutomatically(true);
@@ -121,22 +124,28 @@ public class MainPersenter extends BasePresenter<MainModel, MainView> {
                 if (!view.getSettings().getLoadsImagesAutomatically()) {
                     view.getSettings().setLoadsImagesAutomatically(true);
                 }
+                if (getView() == null) {
+                    return;
+                }
                 getView().dismissProgressDialog();
                 super.onPageFinished(view, url);
             }
 
         });
-        getView().getWebView().addJavascriptInterface(this, "efunboxJS");
+        getView().getWebView().addJavascriptInterface(new MainJsMethod(), "efunboxJS");
     }
 
 
     public void openWeb(Intent intent) {
         getView().getWebView().loadUrl("");
         mLoadUrl = model.getDefaultWebUrl();
-        String intent_url = intent.getStringExtra("url");
-        Logger.e("-----------------------------");
-        Logger.e("intent_url:" + intent_url);
-        Logger.e("-----------------------------");
+        Uri uri = intent.getData();
+        String intent_url = "";
+        if (uri != null) {
+            intent_url = uri.getQueryParameter("url");
+        } else {
+            intent_url = intent.getStringExtra("url");
+        }
         if (intent_url != null && !intent_url.isEmpty()) {
             mLoadUrl = intent_url;
         }
@@ -150,11 +159,41 @@ public class MainPersenter extends BasePresenter<MainModel, MainView> {
 
     }
 
+    //--------------------------------------------------------JS方法
+
+    class MainJsMethod {
+        @JavascriptInterface
+        public void closeApp() {
+            android.os.Process.killProcess(Process.myPid());
+        }
+
+
+        /*
+         * 根据key设置SP参数方法
+         * */
+        @JavascriptInterface
+        public void setPreData(String key, String json) {
+            SharedPerfenceUtil.setPrefString(ContextUtil.getmApplicationContext(), key, json);
+        }
+
+
+        /*
+         * 根据key获取SP参数方法
+         * */
+        @JavascriptInterface
+        public String getPreData(String key) {
+            return SharedPerfenceUtil.getPrefString(ContextUtil.getmApplicationContext(), key, "");
+        }
+
+        /*
+         * 根据key删除SP参数
+         * */
+        @JavascriptInterface
+        public void delPreData(String key) {
+            SharedPerfenceUtil.clearRecourds(ContextUtil.getmApplicationContext(), key);
+        }
+
 
-    @SuppressLint("JavascriptInterface")
-    @JavascriptInterface
-    public void closeApp() {
-        model.closeApp();
     }
 
 }

+ 31 - 0
app/src/main/java/com/edufound/ott/zijie/yuwen/receiver/HomeKeyEventReceiver.java

@@ -0,0 +1,31 @@
+package com.edufound.ott.zijie.yuwen.receiver;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.text.TextUtils;
+
+import com.edufound.ott.zijie.yuwen.util.Logger;
+
+public class HomeKeyEventReceiver extends BroadcastReceiver {
+
+    String SYSTEM_REASON = "reason";
+    String SYSTEM_HOME_KEY = "homekey";
+    String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
+    public static boolean isClickHome = false;
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        // TODO Auto-generated method stub
+        String action = intent.getAction();
+        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
+            String reason = intent.getStringExtra(SYSTEM_REASON);
+            if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {
+                Logger.e("click home");
+                android.os.Process.killProcess(android.os.Process.myPid());
+            } else if (TextUtils.equals(reason, SYSTEM_DIALOG_REASON_RECENT_APPS)) {
+            }
+        }
+    }
+
+}

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

@@ -7,12 +7,4 @@
         <item name="colorAccent">@color/colorAccent</item>
     </style>
 
-    <style name="AppStartTheme" parent="Theme.AppCompat.Light.NoActionBar">
-        <!-- <item name="android:windowIsTranslucent">true</item>-->
-        <!-- 兼容API 7-->
-        <item name="android:windowActionBar">false</item>
-        <item name="android:windowBackground">@drawable/efunbox_window_bg</item>
-        <item name="android:windowNoTitle">true</item>
-        <item name="android:windowFullscreen">true</item>
-    </style>
 </resources>