How to call getResources() from a class which has no context?

后端 未结 5 1037
借酒劲吻你
借酒劲吻你 2020-12-02 16:27

In my application I have many classes and activities. Droid is a class which does not have context. Mygame is a class which extends SurfaceView and implements SurfaceHolder.

相关标签:
5条回答
  • 2020-12-02 17:14

    A Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on. It is an "interface" that allows access to application specific resources and class and information about application environment. Your activities and services also extend Context to they inherit all those methods to access the environment information in which the application is running.

    This means you must have to pass context to the specific class if you want to get/modify some specific information about the resources. You can pass context in the constructor like

    public classname(Context context, String s1) 
    {
    ...
    }
    
    0 讨论(0)
  • 2020-12-02 17:16

    Example: Getting app_name string:

    Resources.getSystem().getString( R.string.app_name )
    
    0 讨论(0)
  • 2020-12-02 17:16

    It can easily be done if u had declared a class that extends from Application

    This class will be like a singleton, so when u need a context u can get it just like this:

    I think this is the better answer and the cleaner

    Here is my code from Utilities package:

     public static String getAppNAme(){
         return MyOwnApplication.getInstance().getString(R.string.app_name);
     }
    
    0 讨论(0)
  • 2020-12-02 17:22

    The normal solution to this is to pass an instance of the context to the class as you create it, or after it is first created but before you need to use the context.

    Another solution is to create an Application object with a static method to access the application context although that couples the Droid object fairly tightly into the code.

    Edit, examples added

    Either modify the Droid class to be something like this

     public Droid(Context context,int x, int y) {
        this.bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.birdpic);
        this.x = x;
        this.y = y;
        }   
    

    Or create an Application something like this:

    public class App extends android.app.Application
    {
        private static App mApp = null;
        /* (non-Javadoc)
         * @see android.app.Application#onCreate()
         */
        @Override
        public void onCreate()
        {
            super.onCreate();
            mApp = this;
        }
        public static Context context()
        {
            return mApp.getApplicationContext();
        }
    }
    

    And call App.context() wherever you need a context - note however that not all functions are available on an application context, some are only available on an activity context but it will certainly do with your need for getResources().

    Please note that you'll need to add android:name to your application definition in your manifest, something like this:

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:name=".App" >
    
    0 讨论(0)
  • 2020-12-02 17:22

    This always works for me:

    import android.app.Activity;
    import android.content.Context;
    
    public class yourClass {
    
     Context ctx;
    
     public yourClass (Handler handler, Context context) {
     super(handler);
        ctx = context;
     }
    
     //Use context (ctx) in your code like this:
     block1 = new Droid(BitmapFactory.decodeResource(ctx.getResources(), R.drawable.birdpic), 100, 10);
     //OR
     builder.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.drawable.birdpic));
     //OR
     final Intent intent = new Intent(ctx, MainActivity.class);
     //OR
     NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
     //ETC...
    
    }
    

    Not related to this question but example using a Fragment to access system resources/activity like this:

    public boolean onQueryTextChange(String newText) {
     Activity activity = getActivity();
     Context context = activity.getApplicationContext();
     returnSomething(newText);
     return false;
    }
    
    View customerInfo = getActivity().getLayoutInflater().inflate(R.layout.main_layout_items, itemsLayout, false);
     itemsLayout.addView(customerInfo);
    
    0 讨论(0)
提交回复
热议问题