Trak_It HOW TO: use LocationService extension with a GoogleSpreadsheet to record real time locations while the Trak_It app is in the background (also works in the foreground).
Edit Sept. 2022. Be aware the example Blocks does not work with Version 2 of the Location Service extension. The example code only works if you use Version 1 and nb189 and Companion 2.64. @Taifun now provides Blocks showing how the tutorial can be used with Version 3 of the LocationService extension (see the example at App Inventor Extensions: Location Service | Pura Vida Apps )** and a Google Sheet.
Trak progress over your route using your Android cell in real time using the location sensor built-in to the extension, even when the app goes to sleep using Taifun's LocationService extension . Use Tim's advice showing how to use PUT with Google Sheets step by step to post the coordinates to the Google Sheet .
Here are all the Blocks. Really!
The example uses a paid extension; sorry no aia. Create an appropriate spreadsheet following Tim's instructions. The example app is an adaptation of the template aia Tim provides modified to use the Blocks from the extension. A similar app to post coordinates can be built without an extension using native controls. See the comments at the end of this tutorial; it will work only in the foreground.
Build the Beacon app. How to do much of this is described in detail in Tim's link.
You need to
- create a Google Spreadsheet on your Google Drive
- name the spreadsheet and add a script using the script editor.
- save the spreadsheet with Sharing.
- obtain and load the Location Service extension. This is a paid extension.
- add the Blocks and note the example is similar to Tim's app template aia but is very different too. Follow the Blocks to build the app.
- a WIFI or network connection (to access the spreadsheet).
When you trak and the service is engaged, the location sensor in the extension determines your location. It posts (Broadcasts using a PUT) the location coordinates and a timestamp continuously to a spreadsheet. See Taifun's documentation showing how to do something similar using MySQL. The app acts as a location Beacon; posting data to a Google spreadsheet hosted on your Google Drive.
Monitor the movement (location coordinates) of the Android that is running the example code in real time by observing values posted into the spreadsheet.
Easily use your PC to view the spreadsheet as it updates or use a separate Android app that monitors the spreadsheet and posts the data to a map. Yes, you need to build the monitoring app using the advice in Tim's PUT document (see link above).
The app posts location changes every 30000 ms (1/2 minute). I used this interval to keep the testing times to a minimum and demonstrate the extension really works with the spreadsheet. Latitude, Longitude and a timestamp is provided. In the real world, there is usually no reason to post this frequently, the app will use up a lot of bandwidth if you are using network to access the Web and post too frequently.
Remember to copy your Spreadsheet script link to the scriptURL block when you code the Blocks.
Label1 and Label2 allow you to monitor the present location coordinates while the app is awake.
The Button1 event handler configures the location service and initiates the tracking.
The Button2 event handler stops the app from tracking. Use Button2 to shut the app down; without it, the app will continue to trak until the Android operating system shuts it down.
Does the app sound an alarm when it reaches a destination. No.
Does the app make a map. No, it only beacons. You can add a map to display while the app is awake.
Does it track and post coordinates with the app running in the foreground or background? Yes but the service must be turned on (Button1).
The example provides the minimum code it takes to run and post coordinates to the spreadsheet while the app is in the background and running the service. Add you own enhancements.
Enhancements:
- add a Map component to show the current location or a LineString trak of locations on the map. Use a Marker or a LineString of the example Trak_It Beacon app when the app is in the foreground.
- create a separate Monitoring app to poll the spreadsheet. Use the data to post current location on a map. You can't use the service Broadcast app (how can you view it when it is hibernating?) so you might need a separate monitoring app on a separate device.
- Determine when you are close to or reached a destination. Code a ring fence or calculate a distance remaining to a specific location on the Monitoring app.
- add more data fields to your spreadsheet. Currently LATITUDE, LONGITUDE and DATETIME are posted. You can also add CURRENTADDRESS. (See Taifun's example using a MySQL spreadsheet and a Google Spreadsheet)
Thanks to @Taifun and @TIMAI2 for the tools to make this app example possible.
Tested Version 1 of the extension on an Android 8.1, 11 and 12. I let the app determine location while the app was in the background for over an hour in my tests. It did indeed work. I only tested with WIFI. Will the app work with network? Probably. I get a similar app working using Version 3 of the extension.
Consider using the example app along with another user who has a Monitoring app capturing the spreadsheet data ( in a CSV file and posting to a map perhaps.
Other resources:
Using extensions App Inventor Extensions
Other options:
Use a regular LocationSensor and a StayAwake routine and the Web component to make your app. An app made that way can share the 'LocationService' spreadsheet you use with Trak_It.
Here is how you might code a Stay Awake block routine using a Notifier
or you could use Taifun’s Tools Extension: https://puravidaapps.com/tools.php
Using either an extension or a block routine drains your device battery rapidly . These work around methods are not running the app in the Background, they force the app screen to stay open .
===========================================================================
WARNING Google considers apps that developers use with Sharable Google Sheets on Google Drive a security issue. Is it? You have to decide. Use at your own risk. I am comfortable using this for my own use.
===========================================================================
Regards,
Steve