Category Archives: Uncategorized

Timers in Android

One thing that’s always been curious to me is the lack of a simple timer class in Android. Instead you need to work around the lack in one of two ways. The easiest way is via a Handler. Its not hard to do, but its a little bit of busy work. The problem with this is that it’s only appropriate for very short timers or for things tied heavily to the UI. Its a non-exact timer and may be delayed indefinitely if the activity is paused or the phone goes idle. It won’t last beyond the Context that sets it at all. The other way is via an alarm. Alarms are persistent across Contexts and may be fairly exact. But they’re based on BroadcastReceivers, which can be tricky to work with- since the app may be started to process the alarm and call to the BroadcastReceiver, you have to code it carefully to get any data to persist until the broadcast occurs. In addition, Google keeps changing the AlarmManager API with respect to how exact timers are and correctly using it is a bit tricky.
Continue reading

Two level caching

This post builds off my post for disk caching. Sometimes you want a disk cache, to prevent unneeded network requests. Sometimes you want a memory cache, to put a cap on the amount of RAM you use. Sometimes you want both- you want a small fixed size working set in RAM, and a larger pool of objects saved to disk in case they’re needed. The solution is two level caching. The idea is that the memory cache is a subset of the disk cache, allowing you to page in from the disk cache to the memory cache as needed. Given you already have a disk cache (see previous post) the amount of extra code is pretty trivial. So I’ll skip the in depth discussion and just leave the solution here.
Continue reading

Disk caching on Android

Frequently when writing Android apps I’ll find the need for a cache. The classic case is with Bitmaps- adding all your bitmaps to a cache and grabbing them solely from the cache can solve a lot of OOM issues. And Google has written a great class, LRUCache, to easily enable memory caching. The problem is that memory caching frequently isn’t enough. If I have an app that needs to download lots of images from the net, I may need tens or hundreds of MB of images, and I don’t want to redownload them every time the app restarts or it falls out of a small memory cache. At the same time I don’t want to use up exponentially increasing amounts of disk space. The solution is to use a disk cache.
Continue reading

Why no posts lately

There haven’t been too many posts lately. Why? I’ve recently been relocated from the East Coast to Silicon Valley for work. I’m not free to discuss why that is, but I hope to be putting out some cool new tech soon. Unfortunately between that and my personal life, there hasn’t been time for blog posts. I’ll rectify that soon, but it could be another week or two first.

Reflection and JSON parsing

One of the things I most hate to do in programming is parsing JSON. Not because it’s hard, but because it’s trivial. That’s the problem- it’s boring yet very time consuming. I don’t want to have to write the line myvar = jsonValue.getInt(“myvarname”,0); 1000 times. I want to take a string and just suck it up into an object. Nowdays in Java I would just use GSON, a library by Google that uses reflection to do this and is used widely by the Java community. But there was a time before this library became popular and I actually had the same idea. Since I rarely used reflection (and still rarely use it, I find it’s generally a code smell) I thought it would be a great opportunity to learn. So here it is- my own personal JSON parsing library.

As an aside- I had originally planned on revisiting this code and expanding it for a part 2 on this, but I’ve decided there’s no value here over using GSON. So I’ll take some of the features I planned to add and use them elsewhere in the future, in a bit of a broader way. As a result this update is going to be lower in detail and more in code, since I have no interest in keeping this library updated and will be moving my libraries that depend on it to GSON. I’m posting this mainly to increase my ready to go post count and to provide an interesting example into using reflection.

Continue reading

A failed app

Two weeks ago I failed. I came up against a program I could not write. It took the laws of physics itself to break me, but it happened. I’m disappointed my app couldn’t be made to work, but I’m reminded that no time spent programming is wasted.

Continue reading