Get current activity Context in class without activity

I have an android application that doesn't have one. activities. Now I need the context of the current work in the class without activity. How can i get this.

+8
android-activity android-context
source share
1 answer

You can use the Application class:

 public class MyApplication extends Application { private static Context context; private static Activity activity; public void onCreate() { super.onCreate(); MyApplication.context = getApplicationContext(); } public synchronized static Context getAppContext() { return MyApplication.context; } /** * setCurrentActivity(null) in onPause() on each activity * setCurrentActivity(this) in onResume() on each activity * */ public static void setCurrentActivity(Activity currentActivity) { activity = currentActivity; } public static Activity currentActivity() { return activity; } } 
+16
source share

All Articles