forked from Sunzxyong/Tiny
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
郑晓勇
committed
May 23, 2017
1 parent
0c9133c
commit e8d8275
Showing
4 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
tiny/src/main/java/com/zxy/tiny/common/ApplicationLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.zxy.tiny.common; | ||
|
||
import android.app.Application; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* Created by zhengxiaoyong on 2017/5/23. | ||
*/ | ||
public class ApplicationLoader { | ||
|
||
private static Application sApplication; | ||
|
||
public static Application get() { | ||
return sApplication != null ? sApplication : getApplication(); | ||
} | ||
|
||
private static Application getApplication() { | ||
Application application = null; | ||
Method method; | ||
try { | ||
method = Class.forName("android.app.AppGlobals").getDeclaredMethod("getInitialApplication"); | ||
method.setAccessible(true); | ||
application = (Application) method.invoke(null); | ||
} catch (Exception e) { | ||
try { | ||
method = Class.forName("android.app.ActivityThread").getDeclaredMethod("currentApplication"); | ||
method.setAccessible(true); | ||
application = (Application) method.invoke(null); | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
return application; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters