0

I need to make my app aware on user's location in all activies, fragments and services running in FG and BG. Also, it is mandatory to have location in China too, so Google Play Service's FusedLocation doesn't seem to be an option.

Is there a way to make the app aware on location changes no matter the context without creating a listener in every act/frag/serv and attaching and detaching in lifecycle?

2 Answers2

0

Sounds like you want to extend the application class. By doing this global variables and methods can be exposed. Sorry the link is not a very good example for your particular case, maybe you can find better examples. Update the tag in the manifest android:name=MyExtendedApplicationClass

Pomagranite
  • 696
  • 5
  • 11
  • Is Application available also when the service is running in background? – Adrián García Campos Dec 14 '17 at 14:59
  • Good point. You could use an aidl interface to communicate with a service but that would complicate not simplify. https://stackoverflow.com/questions/12381977/starting-a-service-from-application-class or getapplicationcontext from the service? Google location api is called from ui thread, right? So I don't think you will have a problem but I could be wrong. Something to consider anyway. – Pomagranite Dec 14 '17 at 15:25
0

For maps in China you can use, https://www.mapbox.com/.

Note: They have a test app on play store, check it out before using the SDK.

To make your app location aware without boilerplate code, take a look at bound services in android. These are basically services that run in background and give output from wherever they are started.

Take a look at code examples in android develop docs Bound Services.

You can have a BaseActivity which has access to the location bound services. Your remaining activities can inherit from this BaseActivity. I hope you will be able to figure out the rest because exactl code for your problem would be too huge to post as an answer.

Mayur Rokade
  • 512
  • 7
  • 20