0

I have developed a syncadapter class as specified on the developer site.

I am frequently sending data to the server using onPerformSync() every 1 hour. The problem I am facing is I am not able to use getResources().getString(R.string.domain_name) or getResources().getInteger(R.integer.port) to access my resources from inside onPerformSync().

I wanted to know if there is a way to access these resources available in the /res folder.

vjdhama
  • 4,878
  • 5
  • 33
  • 47
Roadblock
  • 2,041
  • 2
  • 24
  • 38

1 Answers1

2

You could try to get Application Context which may be useful through out your Application.

Code Snippet:-

public class MyApp extends android.app.Application {
private static MyApp instance;
public MyApp() {
    instance = this;
 }
public static Context getContext() {
    return instance;
  }
}

Now you could use Application Context to get your resources throughout your application which means inside your performSync() too.

  MyApp.getContext().getResources().getString(R.string.domain_name)

You need to add your Application class inside your <application> in AndroidManifest.xml

 <application
    android:name="com.example.MyApp"
    android:label="@string/app_name" >
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
  • I already tried that, but the domain_name doesn't even come up in suggestions. You can get suggestions till R.string. only. – Roadblock May 05 '14 at 12:45
  • @Roadblock : If `domain_name` means your resources not coming up in suggestion that means your `R.java` is not getting generated. It's Auto generated file. Please check your `Error Log` if using eclipse. Try to check your `/res` error folder for auto generating `R.java`. – Vikalp Patel May 05 '14 at 12:48
  • All I ever hear about this is that you shouldn't do it, I do it and I feel really guilty about how easy it makes things- you can have static function that returns the instance, ```public static Application app(){return instance;}``` and then anywhere you need a context in your application you can pretty much just call ```app()```, but I can pretty much guarantee that this is a horrible and bad thing to do – Saik Caskey Jun 01 '18 at 12:22