8000 Update AutoSizeUtils.java · JessYanCoding/AndroidAutoSize@45fd66f · GitHub
[go: up one dir, main page]

Skip to content

Commit 45fd66f

Browse files
Update AutoSizeUtils.java
1 parent 972374d commit 45fd66f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

autosize/src/main/java/me/jessyan/autosize/utils/AutoSizeUtils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,26 @@ public static int in2px(Context context, float value) {
5252
public static int mm2px(Context context, float value) {
5353
return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, value, context.getResources().getDisplayMetrics()) + 0.5f);
5454
}
55+
56+
private static Application getApplicationByReflect() {
57+
try {
58+
@SuppressLint("PrivateApi")
59+
Class<?> activityThread = Class.forName("android.app.ActivityThread");
60+
Object thread = activityThread.getMethod("currentActivityThread").invoke(null);
61+
Object app = activityThread.getMethod("getApplication").invoke(thread);
62+
if (app == null) {
63+
throw new NullPointerException("you should init first");
64+
}
65+
return (Application) app;
66+
} catch (NoSuchMethodException e) {
67+
e.printStackTrace();
68+
} catch (IllegalAccessException e) {
69+
e.printStackTrace();
70+
} catch (InvocationTargetException e) {
71+
e.printStackTrace();
72+
} catch (ClassNotFoundException e) {
73+
e.printStackTrace();
74+
}
75+
throw new NullPointerException("you should init first");
76+
}
5577
}

0 commit comments

Comments
 (0)
0