diff --git a/AutoEx-Lib/src/main/java/com/bolex/autoEx/AutoEx.java b/AutoEx-Lib/src/main/java/com/bolex/autoEx/AutoEx.java index e6a9b49..f6cb9f3 100644 --- a/AutoEx-Lib/src/main/java/com/bolex/autoEx/AutoEx.java +++ b/AutoEx-Lib/src/main/java/com/bolex/autoEx/AutoEx.java @@ -3,8 +3,8 @@ import android.app.Application; import android.content.Context; import android.content.Intent; +import android.util.Log; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -14,7 +14,7 @@ public class AutoEx implements AutoExConstant { - private Context mApp; + private static Context mApp; private Thread.UncaughtExceptionHandler mUEH; public static int maxSize = 4; public static String tag = LOG_TAG; @@ -25,9 +25,11 @@ public class AutoEx implements AutoExConstant { * */ public static void apply() { - Application app = getApp(); - if(app!=null){ + Context app = getApp(); + if (app != null) { apply(app, AutoEx.maxSize); + } else { + Log.e(tag, AutoEx.class.getName(), new RuntimeException(ANDROID_P_ERROR)); } } @@ -67,8 +69,13 @@ public static void apply(Context mApp, int maxSize, String tag, boolean isDebug) return; } if (autoEx == null) { - autoEx = new AutoEx(); - autoEx.init(mApp, maxSize, tag, isDebug); + synchronized (AutoEx.class) { + if (autoEx == null) { + autoEx = new AutoEx(); + autoEx.init(mApp, maxSize, tag, isDebug); + } + } + } } @@ -98,16 +105,19 @@ private String getErrorMsg(Throwable ex) { return message.substring(message.indexOf(":") + 2); } - private static Application getApp() { - Class clazz = null; - try { - clazz = Class.forName("android.app.ActivityThread"); - Method method = clazz.getDeclaredMethod("currentApplication"); - Application mApp = (Application) method.invoke(null); + private static Context getApp() { + if (mApp != null) { return mApp; - } catch (Exception e) { - e.printStackTrace(); + } else { + try { + Class clazz = Class.forName("android.app.ActivityThread"); + Method method = clazz.getDeclaredMethod("currentApplication"); + Application mApp = (Application) method.invoke(null); + return mApp; + } catch (Exception e) { + e.printStackTrace(); + } + return null; } - return null; } } diff --git a/AutoEx-Lib/src/main/java/com/bolex/autoEx/AutoExConstant.java b/AutoEx-Lib/src/main/java/com/bolex/autoEx/AutoExConstant.java index dcd1644..c5b9911 100644 --- a/AutoEx-Lib/src/main/java/com/bolex/autoEx/AutoExConstant.java +++ b/AutoEx-Lib/src/main/java/com/bolex/autoEx/AutoExConstant.java @@ -21,4 +21,5 @@ interface AutoExConstant { String ERROR_MSG = "ERROR_MSG"; String LOG_TAG = "AutoEx"; String MAX_SIZE = "MAX_SIZE"; + String ANDROID_P_ERROR = "由于Android版本差异化导致反射不可用,AutoEx.apply()请调用多个参数的构造函传入ConText对象"; } diff --git a/AutoEx-Lib/src/main/java/com/bolex/autoEx/DoHandleService.java b/AutoEx-Lib/src/main/java/com/bolex/autoEx/DoHandleService.java index 4f231c2..61e7615 100644 --- a/AutoEx-Lib/src/main/java/com/bolex/autoEx/DoHandleService.java +++ b/AutoEx-Lib/src/main/java/com/bolex/autoEx/DoHandleService.java @@ -32,35 +32,8 @@ private void search(final String errorMsg, int maxSize) { HttpUtil.doGet(API.SEARCH_URL + API.PAGESIZE + maxSize + API.Body + errorMsg.replace(" ", "%20"), new HttpUtil.HResponse() { @Override public void onFinish(String msg) { - StringBuffer log = new StringBuffer(); - log.append(LINE_START); - try { - JSONObject stackoverflowData = new JSONObject(msg); - JSONArray items = stackoverflowData.getJSONArray(JSON_ITEMS); - int length = items.length(); - if (length > 0) { - log.append(String.format(AUTO_ERROR_TYPE, errorMsg)); - log.append(String.format(AUTO_RECOMMEND, length)); - log.append(MI_LINE); - for (int i = 0; i < length; i++) { - JSONObject item = (JSONObject) items.get(i); - String link = item.getString(JSON_LINK); - String title = item.getString(JSON_TITLE); - log.append(String.format(AUTO_LINKS, title, link)); - if (i < length - 1) { - log.append(MI_LINE); - } - } - } else { - log.append(SORRY); - } - log.append(LINE_END); - log(log); - } catch (JSONException e) { - log.append(String.format(AUTO_ERROR, e.getMessage())); - log.append(LINE_END); - log(log); - } + resolveLog(msg, errorMsg); + stopSelf(); } @Override @@ -70,10 +43,43 @@ public void onError(String error) { log.append(String.format(AUTO_ERROR, error)); log.append(LINE_END); log(log); + stopSelf(); } }); } + private void resolveLog(String msg, String errorMsg) { + StringBuffer log = new StringBuffer(); + log.append(LINE_START); + try { + JSONObject stackoverflowData = new JSONObject(msg); + JSONArray items = stackoverflowData.getJSONArray(JSON_ITEMS); + int length = items.length(); + if (length > 0) { + log.append(String.format(AUTO_ERROR_TYPE, errorMsg)); + log.append(String.format(AUTO_RECOMMEND, length)); + log.append(MI_LINE); + for (int i = 0; i < length; i++) { + JSONObject item = (JSONObject) items.get(i); + String link = item.getString(JSON_LINK); + String title = item.getString(JSON_TITLE); + log.append(String.format(AUTO_LINKS, title, link)); + if (i < length - 1) { + log.append(MI_LINE); + } + } + } else { + log.append(SORRY); + } + log.append(LINE_END); + log(log); + } catch (JSONException e) { + log.append(String.format(AUTO_ERROR, e.getMessage())); + log.append(LINE_END); + log(log); + } + } + private void log(StringBuffer log) { Log.e(AutoEx.tag, log.toString()); } diff --git a/README.md b/README.md index db81be2..d1f59a9 100644 --- a/README.md +++ b/README.md @@ -33,37 +33,30 @@ ## 用法 - - 1.根build.gradle添加仓库: + - 根build.gradle: ``` allprojects { repositories {maven { url 'https://jitpack.io' }} } ``` - - 2.添加dependency (低版本的Gradle可以使用 debugCompile releaseCompile) + - dependency (低版本Gradle使用 debugCompile releaseCompile) ``` dependencies { - debugImplementation 'com.github.BolexLiu.AutoEx:AutoEx-Lib:v1.0.7' //debug包 - releaseImplementation 'com.github.BolexLiu.AutoEx:AutoEx-Lib-No-Op:v1.0.7' //release包 + debugImplementation 'com.github.BolexLiu.AutoEx:AutoEx-Lib:v1.0.8' + releaseImplementation 'com.github.BolexLiu.AutoEx:AutoEx-Lib-No-Op:v1.0.8' } ``` - - 3.Application初始化一行搞定 + - 初始化方式可选(建议放在在Application中) ```java - AutoEx.apply(); + AutoEx.apply();//一行即可 ``` - 或者你也可以自己配参数 ```java - /** - * @param mApp Applicatin - * @param maxSize 最大提示答案数目 - * @param tag 自定义日志 默认 AutoEx - * @param isDebug 是否开启调试 true为开启 false为关闭 默认开启 - */ - public static void apply(Context mApp, int maxSize, String tag, boolean isDebug) + AutoEx.apply( mApp, maxSize, tag, isDebug)//Applicatin 答案数目 日志TAG 是否调试 ``` @@ -79,6 +72,7 @@ dependencies { - 1.0.5 修复小米等手机直接被Kill,添加多进程(感谢静心同学的测试) - 1.0.6 最大提示数目、增加日志过滤、调试模式选择 - 1.0.7 增加无参构造、增加Rlease空包支持(感谢巴神) +- 1.0.8 对无法反射获取Context的机型给出日志提示 ## 后续需求 @@ -88,13 +82,13 @@ dependencies { -**AutoEx**仅不到9kb的大小。为了保持精简干净不给开发者添麻烦,**AutoEx**放弃依赖第三方库。(比如OKHttp、Gson等)。 -所以你不用担心依赖冲突等问题。另外请保持手机是联网状态的。希望能给你带来方便。欢迎pr,么么哒。 +**AutoEx**只有9kb的大小。为了保持精简干净不给开发者添麻烦,**AutoEx**没有依赖第三方库。(比如OKHttp、Gson等)。 +所以你不用担心依赖冲突等问题。另外请保持手机是联网状态的。希望能给你带来方便。 -[AutoExsvg]:http://img.shields.io/badge/AutoEx-v1.0.7-brightgreen.svg +[AutoExsvg]:http://img.shields.io/badge/AutoEx-v1.0.8-brightgreen.svg [AutoEx]:https://github.com/BolexLiu/AutoEx [api+svg]:http://img.shields.io/badge/API-14+-brightgreen.svg