GetResources () from a static method, without context

I am working on SQLiteOpenHelper , from which I will read databases using static methods (since the databases are still partitioned anyway). Is it possible to get the application context something like this:

 public static final Context context = XXX; 

Should it be possible right? Since I obviously only call from the current application, and both resources and databases are used inside the application.

To be clear: I want to access resources and SQLiteDatabases (if I'm wrong in the context of the contextual approach).

Is it possible to achieve?

Edit: Is it possible to get a context from inside something like this (without passing it as a parameter).

 public class foo{ foo(){ XXX.getResources(); } } 

Edit2: @Britzl's attempt: first idea

 public class SpendoBase extends Application{ private static Context context; public SpendoBase(){ System.out.println("SPENDOBASE: " + this); System.out.println("SPENDOBASE: " + this.getBaseContext()); } public static Context getContext(){ return this.context; } } 

How do I understand the context? Either in the constructor or in the form of getContext();

Ps the getBaseContext() returns null, and getApplicationContext a nullPointerException .

0
android android-context resources
Apr 29 '13 at 11:02
source share
1 answer

I see three possible solutions to your problem:

  • Create your own subclass of the application and set it as your application class in the manifest file. In your subclass, you may have a static getInstance () method that will provide you with the application context (and therefore resources) from anywhere in your application. Example:

     public class BaseApplication extends Application { private static BaseApplication instance; public BaseApplication() { super(); instance = this; } public static BaseApplication getInstance() { return instance; } } 

    And in AndroidManifest.xml:

     <application android:name="com.example.BaseApplication" ...> ...activities </application> 
  • Pass context for any calls you make in your SQLiteOpenHelper

  • Deploy a resource instance using dependency injection

+1
Apr 29 '13 at 11:09
source share



All Articles