Created
August 22, 2017 04:31
-
-
Save ok3141/89c3ad6215cf2bd5d8c76f24171b459e to your computer and use it in GitHub Desktop.
Android. Get static Context
This file contains hidden or 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
| import android.annotation.SuppressLint; | |
| import android.content.Context; | |
| import android.support.annotation.Nullable; | |
| @SuppressLint("PrivateApi") | |
| public class StaticContext { | |
| @Nullable | |
| public static Context getContext() { | |
| try { | |
| return (Context) Class.forName("android.app.ActivityThread") | |
| .getDeclaredMethod("currentApplication") | |
| .invoke(null); | |
| } catch (Throwable ex) { | |
| return null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment