Reputation: 13
I have a service in my app and I what to access to the strings.xml file that contain what I need.
I want to get a string that are in the strings.xml but I can't acces to this file in my service.
I think that the problem deals with the context but I can't resolve it.
There is an extract of my service:
package com.receiver;
protected Void doInBackground() {
// GPS timeout
final int maxSeconds = 60; //30
int count = 0;
// If we've finished finding location or exceeded timeout,
// break the loop.
while (findingLocation && count < maxSeconds) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// Continue if interrupted
}
// Increment the timer by one second
count++;
}
// If we're still finding location, switch to network locations.
if (findingLocation) {
locationHandler.sendMessage(new Message());
locationManager.removeUpdates(SMSReceiver.this);
lat = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getLatitude();
lng = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getLongitude();
msg = "";// here I whant to get my string from the strings.xml but I can't access to this file
} else {
lat = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude();
lng = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude();
msg = " "; // here I whant to get other string from the strings.xml but I can't access to this file
}
Thanks for your help.
Sorry for my bad english.
Upvotes: 1
Views: 771
Reputation: 688
Are you taking about and Android service? From you code snippet it looks like you want to access a string from an AsyncTask.
For a regular Android service, you can use the getString()
method from Context
since a Service
extends Context
. Simply pass in the resource ID of the string you want, and you should be all set.
http://developer.android.com/reference/android/content/Context.html#getString(int)
In order to access the string from an AsyncTask
, I usually just pass the ApplicationContext to the AsyncTask
, then I can access anything I want. Keep in mind that passing around Contexts can be problematic so, you might want to pass the actual string to the AsyncTask
so it does not need a Context
reference.
Upvotes: 3