gutenachtsteph
gutenachtsteph

Reputation: 1

Android: Communication of different Activities

I have a question (not code based) regarded to communication between different app components.

I want to write a "big" Logging/Tracking tool. This tool does have about 3 Parts.

  1. Part, my main activity. I wanna start/stop tracking/logging at this point via clicking/declicking a checkbox
  2. Part, a location part, in which I wanna track the Location History of a user (via Location API)
  3. Part, a Sensor logging part, in which I Track all sensor data I can get from a users smartphone.

In part 2 and part 3 I wanna set the settings of Location API (e.g. Accuracy and refresh time) and Sensor API, additionally in part 3 I wanna activate/deactivate different Sensors for logging.

In part 1, my main activity, I wanna start the complete logging/tracking.

It's more explained in the attached picture.

I've no problem with coding in JAVA. My problem is, I'm new in Android. I don't have any problem with one Activity. But with more than one... uuuh. I dont understand the communication between my MainActivity and for example Location Activity (names regarded to the picture)

In JAVA I would have my own classes Location/Sensor in which I would store my logic.

After click on "Click to turn on tracking" I would create 2 Threads (for Location and Sensors) in which tracking would start. Both would save their data in Lists and after I stop tracking it would be written in textfiles as I want it.

And in Android? In my special case? Do I have 3 Activities? Do I communicate via Intents? It's a bit confusing because in this case all of these Activites would have their own onCreated()/onStop()/... Methods. So I do need to seperate my tracking/logging logic from my Activities because I only wanna choose/set my settings and would close my Location/Logging Activity to turn back to my MainActivity after that.

Would be great if you could recommend a tutorial or help me otherwise.

enter image description here

Upvotes: 0

Views: 98

Answers (1)

Jukka Raanamo
Jukka Raanamo

Reputation: 451

Assuming you wish to receive location and sensor events even when your app is in the background, what I'd suggest is:

  • Keep your tracking settings in some persistent storage, e.g. SharedPreferences, your configuration screens (Activity/Fragment) just read and write these settings
  • In your main activity, when checkbox is checked, read latest configuration, start tracking and also persist that your tracker is "on"
  • To request locations, use methods that take a PendingIntent as argument, e.g. http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(long, float, android.location.Criteria, android.app.PendingIntent) and set target of the PendingIntent to an IntentService where you process and persist locations
  • To request sensor events, you need an active listener. Create a separate Service that is e.g. invoked with regular interval via AlarmManager to collect a sample of sensor data, which can then be persisted

Remember that when doing things in the service (e.g. listening to sensor events) the device's application processor may go to sleep and you will stop receiving events. To prevent this you would need to acquire a WakeLock but be careful not to drain the battery. Also, when device is restarted, you'll need re-enable your location/alarm listeners.

At the end, you might end up with a architecture like this:

  • UI: Activities that edit tracking settings and enable/disable tracking
  • TrackingHelper: a class with logic to enable/disable tracking - registers location listener and alarms that triggers SensorService to collect some sensor data
  • SensorService: Service that registers to listen to sensor events for a requested interval and then stops itself
  • LoggerService: an IntentService that receives Location objects (and maybe other data) and persists them
  • BootReceiver: a BroadcastReceiver that calls TrackingHelper.startTracking (if tracking is enabled)

Upvotes: 1

Related Questions