File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
autosize/src/main/java/me/jessyan/autosize/utils Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments