Database Reference
In-Depth Information
system that cause the onHandleIntent(…) method to be invoked
periodically. The stop() method cancels the alarm using the handle saved
when the alarm was registered. The log is captured and sent in the
onHandleIntent(…) method, but notice that the actual transmission of
the record that occurs in the transmit(…) method is wrapped in an
AsyncTask . The reason for this additional complexity is that
onHandleIntent(…) is invoked on the event handling thread, and we
want to avoid stalling the event handling thread. Even for a background
service, it is important to avoid blocking on the event handling thread
because that would make the service appear to be unresponsive to the OS.
This background logging service is not complex because the data is
generated in the background service and on a fixed schedule. A useful
exercise would be to modify the implementation so that it can batch records
rather than transmit each one independently.
Next turn your attention to the buildRecord() method that collects the
data to be logged. Observe that we use the Android JSON library to
construct a JSONObject with the data. This is convenient because it ends
up in a format that is directly compatible with what the BigQuery API
expects. The table schema we are targeting is shown in Listing 8.2 . In the
appendToLog(JSONObject record) method, we append the records to
a log file on the device. This is convenient for testing because the resulting
file can be extracted from the virtual device and loaded into BigQuery using
the load operation because the log file is correctly formatted as
newline-delimited JSON. If the load job succeeds, you know that you are
generating records with the correct fields for the table you are aiming to fill.
Listing 8.2 : Log table schema
[
{"name": "id", "type": "string", "mode": "required"},
{"name": "ts", "type": "timestamp", "mode":
"required"},
{"name": "screen_on", "type": "boolean"},
{"name": "power", "type": "record", "fields": [
{"name": "charging", "type": "boolean"},
{"name": "usb", "type": "boolean"},
{"name": "ac", "type": "boolean"},
{"name": "charge", "type": "float"}
 
 
Search WWH ::




Custom Search