Skip to content

Commit

Permalink
1.0.8 对无法反射获取Context的机型给出日志提示
Browse files Browse the repository at this point in the history
  • Loading branch information
BolexLiu committed May 16, 2018
1 parent d40735f commit 607d424
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 61 deletions.
40 changes: 25 additions & 15 deletions AutoEx-Lib/src/main/java/com/bolex/autoEx/AutoEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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;
Expand All @@ -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));
}
}

Expand Down Expand Up @@ -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);
}
}

}
}

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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对象";
}
64 changes: 35 additions & 29 deletions AutoEx-Lib/src/main/java/com/bolex/autoEx/DoHandleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
Expand Down
28 changes: 11 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 是否调试
```


Expand All @@ -79,6 +72,7 @@ dependencies {
- 1.0.5 修复小米等手机直接被Kill,添加多进程(感谢静心同学的测试)
- 1.0.6 最大提示数目、增加日志过滤、调试模式选择
- 1.0.7 增加无参构造、增加Rlease空包支持(感谢巴神)
- 1.0.8 对无法反射获取Context的机型给出日志提示

## 后续需求

Expand All @@ -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
Expand Down

0 comments on commit 607d424

Please sign in to comment.