Solved

Android: Accessing String resource without Context

Posted on 2013-02-07
3
2,302 Views
Last Modified: 2014-04-01
I have a class (CFunctions.java) that supplies my app with config info taken from strings.xml.  Until now, I have taken the splash page's Context as a basis for finding string resources.  However, I'm now implementing GCM to launch directly from a notification to a screen that's normally 2 or 3 activities deep in the stack.  As such, there is no splash page context to hijack, and I need a way to read strings.xml without context.  I fear I might need to copy the file out of the package and read it back in as an inputstream, substringing what I need.  Please tell me that there's a better way to get string values without a Context.

An SO user says I should use Resources.getSystem().getString(R.string.[string_name]); but this is also breaking (I believe that Resources invisibly requires a Context).

I've also tried this, and no dice.
0
Comment
Question by:StarbucksDrinker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Expert Comment

by:PCableGuy
ID: 38866640
You should be able to access the strings.xml file in any Activity using the this statement. So try using this.getString(R.string.[string_name])

In an area of the program in which you have access to a Context or Application, such as a ListAdapter, you can try these:
context.getString(R.string.[string_name])
application.getString(R.string.[string_name])
0
 
LVL 1

Author Comment

by:StarbucksDrinker
ID: 38868392
I'm afraid I wasn't communicating clearly;  there is no Context available in this situation, and no way that I can find to access a context.  Every method I find to access string resources requires a context either visibly (this.getString()) or invisibly (application.getString()).  I need t o access these resources in a non-Activity class which has no Context of its own.
0
 
LVL 12

Accepted Solution

by:
PCableGuy earned 500 total points
ID: 38870414
You can try making a global class that extends the Application class which can store all the string variables that you want. Example is here: http://androidresearch.wordpress.com/2012/03/22/defining-global-variables-in-android/

Along those lines, you might even be able to do something like this too, so you always have the ability to get a Context. If you do something like this, be aware of Android Application Lifecycle.

import android.app.Application;
import android.content.Context;

public class Global extends Application {

	@Override
    public void onCreate() {
        super.onCreate();    
		//initialize variables
    }
	
	public Context getContext(){
		
		Context C = this.getApplicationContext();
		return C;
	}
	
	
}

// some other class utilizing the Global class
import android.content.Context;


public class SomeClass {

	Context c;
	
	SomeClass(){
			
			Global g = new Global();
			c = g.getContext();
			c.getString(R.string.app_name);
	}
		
}

Open in new window



Eclipse also warned about this:
public Context getApplicationContext ()
Added in API level 1
Return the context of the single, global Application object of the current process. This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.

Consider for example how this interacts with registerReceiver(BroadcastReceiver, IntentFilter):

If used from an Activity context, the receiver is being registered within that activity. This means that you are expected to unregister before the activity is done being destroyed; in fact if you do not do so, the framework will clean up your leaked registration as it removes the activity and log an error. Thus, if you use the Activity context to register a receiver that is static (global to the process, not associated with an Activity instance) then that registration will be removed on you at whatever point the activity you used is destroyed.

If used from the Context returned here, the receiver is being registered with the global state associated with your application. Thus it will never be unregistered for you. This is necessary if the receiver is associated with static data, not a particular component. However using the ApplicationContext elsewhere can easily lead to serious leaks if you forget to unregister, unbind, etc.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question