Posts Tagged ‘Android’

If you are looking for an easy way to store a DatePicker and TimePicker control to an SQLite database, one method is to convert them to a Calendar object and store the combined date and time in milliseconds (a ‘long’ value). Take the following method as an an example:

The date/time API in Java gets a bad reputation but it is not so bad to work with once you get used to it and is fine for doing simple manipulations or calculations without the need to use a third party library. There are various bugs and some performance issue with the native API so if you are doing an application which heavily utilises date/time functionality you might want to look in to using something like Joda Time instead.

The majority of date/time operations in Java are done using the Calendar object. Below are examples of some common date/time operations using the native Java API:

A boot receiver class is useful when you wish to run a piece of code or start a service as soon as the device has finished booting up. This is very simple to implement requiring only a class that derives from BroadcastReceiver and some configuration in the AndroidManifest.xml file.

There is a link to a sample Eclipse project at the top of this article. Below I examine the main elements of creating a boot receiver class. Lets start by looking at the AndroidManifest.xml file:

This is where the majority of the configuration for a boot receiver takes place. The first thing to note is near the top of the file. You can see that you must declare using the “android.permission.RECEIVE_BOOT_COMPLETED” permission. Without this your application will not be notified on boot up and may crash or otherwise fail.

The AlarmManager is a useful system service that allows you to schedule when you want to have your application code run at a certain time. To use this service you need to set an alarm in the form of a PendingIntent and the time (in milliseconds) when you want it to go off. You also have the ability to set up a repeating alarm by specifying the interval in milliseconds of which the alarm should repeat.

When you set alarms using the AlarmManager it must be an intent broadcast. This means you will need a class that derives from BroadcastReceiver in order to handle the messages. This is defined in the AndroidManifest.xml file using the <receiver> tag. Inside this tag you must also define a custom action which will be used when instantiating the broadcast intent. This is used to distinguish your alert from all the other alerts flying around your device.