Database Reference
In-Depth Information
File lastLog = new File(getCacheDir(), LAST_LOG);
if (lastLog.exists()) {
if (!lastLog.delete()) {
Log.e(TAG, "Could not delete old log file: "
+ lastLog.getPath());
return;
}
}
if (!currentLog.renameTo(lastLog)) {
Log.e(TAG, "Could not rotate: " +
currentLog.getPath());
return;
}
}
FileOutputStream log = new
FileOutputStream(currentLog, true);
log.write(record.toString().getBytes());
log.write('\n');
log.close();
}
}
When users interact with an application, there is often a stream of
interesting events developers would like to capture. Shipping these events
from the client to a backend service needs to be done with some care.
Performing blocking network operations on user interface threads is entirely
taboo because it can cause the user interface to become unresponsive. Even
issuing individual asynchronous I/O operations is usually not optimal
because it leads to small, frequent, and concurrent network operations that
each have fixed HTTP and network overheads that can lead to inefficient
usage of network bandwidth.
In Android you can instead implement a background logging service for
your application, as done in the sample application. This allows you to
optimize the transmission of the logs, for example, by batching them into
larger requests, ensuring only a single request is outstanding at any given
time and controlling the rate at which requests are initiated.
In Listing 8.1 you can see the main methods that control the life cycle of the
service. The start(…) method registers periodic alarms with the operating
Search WWH ::




Custom Search