Mark (et. al. interested parties):
Thank you for the example on reflection. I got it working and thought it
might be prudent to share my results w/ the world so the completed code is
below. For the record, this date value (as a string) is being retrieved
from a SQLite database and the function formatDateTime() is used in a custom
adapter for display of the date for each record in a ListView.
This compiles in Eclipse using Android 1.5. This allows me to set a minimum
sdk version of 2, while still utilizing this really nifty date display
function.
----------------------------------------------------------------------------
------------
public class ResourcesList extends ListActivity {
...
int sdk = new Integer(Build.VERSION.SDK).intValue();
private static final long dateMin = 60000;
private static final long dateMax = (86400000 * 2);
private static final int dateFlags = 0;
...
public String formatDateTime(String dateTime) {
if (dateTime == null) {
return "Never";
} else {
String finalDateTime;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
try {
Date date = df.parse(dateTime);
if (sdk < 3) {
finalDateTime = df.format(date);
} else {
long when = date.getTime();
finalDateTime =
DateUtils.getRelativeDateTimeString(this, (when +
TimeZone.getDefault().getOffset(when)), dateMin, dateMax,
dateFlags).toString();
}
} catch (Exception e) {
finalDateTime = "Unknown";
}
return finalDateTime;
}
}
...
}
Thanks,
Nick Owens
VP, ThreeClix
Office: (904) 429-7039
Mobile: (847) 565-9392
After Hours: (904) 540-5830
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Mark Murphy
Sent: Thursday, February 25, 2010 4:23 PM
To: [email protected]
Subject: Re: [android-developers] Re: FormatDateTime
Nick Owens wrote:
> On another note, is it possible to provide a conditional instruction for
> formatting date/time based on the user's Android package:
>
> if (package > 1.1) {
>
> //cooler date time format
>
> } else {
>
> //regular date time format
>
> }
Yes, either via reflection or via conditional class loading.
> Let me guess? It is technically possible, but not in Eclipse since
Eclipse
> won't even let it compile w/ a function that doesn't compile.
No, it should work fine. However, you need to compile for the higher
version.
> Or can I
> build it for Android 1.2 but allow users of 1.1 to download it by
specifying
> a different min-SDK level than it is built for?
Dunno. What's Android 1.2? :-)
> If so, can I write a
> conditional instruction based on the device's SDK level?
Yes, either via reflection or via conditional class loading.
This was discussed here:
http://stackoverflow.com/questions/2312321/how-to-use-contacts-api-for-andro
id-1-x-and-2-x-simultaneously
and here:
http://stackoverflow.com/questions/2044985/android-contactscontract-and-buil
ding-across-multiple-sdk-versions
Some sample projects are here:
http://github.com/commonsguy/cw-advandroid/tree/master/Contacts/Pick/
http://github.com/commonsguy/cw-advandroid/tree/master/Contacts/Spinners/
First one uses reflection, second one uses conditional class loading.
Here are two more samples:
http://github.com/commonsguy/cw-android/tree/master/APIVersions/
--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy
Warescription: Three Android Books, Plus Updates, One Low Price!
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en