Created an Eph class outside of Activity, but how to get the Context to getApplicationContext () method in the following code?

public class Eph { int x1,x2; void cChart { new CopyAssetfiles(".*\\.st1", getApplicationContext()).copy(); SwE sw = new SwE(getApplicationContext().getFilesDir() + File.separator + "/epit"); } } 

    1 answer 1

    1) Pass to Context constructor

     public class Eph { private Context context; int x1,x2; public Eph(Context context) { this.context = context; } void cChart() { new CopyAssetfiles(".*\\.st1", getApplicationContext()).copy(); SwE sw = new SwE(context.getFilesDir() + File.separator + "/epit"); } } 

    2) Inherit from Application

     public class MyApplication extends Application { private static Context context; public void onCreate() { super.onCreate(); MyApplication.context = getApplicationContext(); } public static Context getAppContext() { return MyApplication.context; } } 

    then add in the manifest to the <application>

     android:name="your.package.MyApplication" 
    • Thank you very much! It all worked! - kaaa