Posts Tagged ‘Android’

Android: Storing a DatePicker and TimePicker in SQLite Database

Comments Off

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:

private Calendar createAsCalendar(DatePicker dp, TimePicker tp)
{
    // Convert a DatePicker and TimePicker control in to a calendar object. The time is
    // stored in UTC minutes and unaffected by time zone
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
 
    cal.set(Calendar.YEAR, dp.getYear());
    cal.set(Calendar.MONTH, dp.getMonth());
    cal.set(Calendar.DAY_OF_MONTH, dp.getDayOfMonth());
    cal.set(Calendar.HOUR_OF_DAY, tp.getCurrentHour());
    cal.set(Calendar.MINUTE, tp.getCurrentMinute());
    cal.set(Calendar.SECOND, 0);
 
    return cal;
}

Read the rest of this entry »


Common Operations With Java Calendar API

Comments Off

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:

Read the rest of this entry »


Android: How to Create a Boot Receiver

4 Comments »

Sample Code: Click Here

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:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bytefoundry.BootReceiver"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk android:minSdkVersion="7" />
 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name=".BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <activity
            android:name=".Boot_ReceiverActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

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.

Read the rest of this entry »


Android: How to Use the Alarm Manager Service

1 Comment »

Sample Code: Click Here

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.

Read the rest of this entry »