Skip to content

Instantly share code, notes, and snippets.

@ok3141
Created August 22, 2017 04:31
Show Gist options
  • Select an option

  • Save ok3141/89c3ad6215cf2bd5d8c76f24171b459e to your computer and use it in GitHub Desktop.

Select an option

Save ok3141/89c3ad6215cf2bd5d8c76f24171b459e to your computer and use it in GitHub Desktop.
Android. Get static Context
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