advanced

Is it on? An internet connection checker snippet

SDK Version: 
M3

Most of our apps use the internet in some way, so we made an util class, that we use regularly. To save some bandwidth, I only use it after a connection related exception(ioexception etc) was thrown, but of course checking ahead is a good idea sometimes.

How to add divider items to ListView

SDK Version: 
M3

Today I'm going to show an easy way to add divider items to your ListView.
First off, you need your own ListView Adapter which extends SimpleAdapter. If you don't know how to create a new class for your own SimpleAdapter, please go and visit: http://developer.android.com/resources/samples/ApiDemos/src/com/example/...

Ok, so here's how it looks like:

  1. public class SpecialAdapter extends SimpleAdapter {
  2.         private LayoutInflater mInflater;
  3.         private List<HashMap<String, String>> items;
  4.  
  5.         public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
  6.                 super(context, items, resource, from, to);
  7.                 // Cache the LayoutInflate to avoid asking for a new one each time.
  8.         this.items = items;
  9.         }
  10.  
  11.          @Override
  12.      public boolean areAllItemsEnabled() {
  13.          return false;
  14.      }
  15.  
  16.      @Override
  17.      public boolean isEnabled(int position) {
  18.          boolean enabled = false;

Running code on phone boot

SDK Version: 
M3

Some applications, for example a mail client, needs to run in the background all the time. You may want to run code right after the device booted.


The device is booting

In the Android system BroadcastReaceivers are just for such mechanism, they can listen for defined intents with given parameters, and run code when they receive one, even when your application is not running at the moment. You can define own actions and fire the manually to control your receivers, but there are a lot of predefined broadcast in the system that are fired automatically on certain events.
One of the is the BOOT_COMPLETED action. The complete list is available here.

All you need to do is a class that extends Broadcastreceiver:

  1. public class HelloReceiver extends BroadcastReceiver {
  2.   @Override

Gzipping files on the fly part 1.

SDK Version: 
M3

Today we wanted to test ways, to optimize the download of files to android phones.
The first way we tried, is the most obvious method of using a little less of everything (bandwith, battery, cpu, backlight, time? etc), compression.

Image source.

But what about speed? Is it worth to sacrifice the app's speed for using less bandwith? How much slower is compressing/decompressing files on a phone anyway?
Read on to find out.

Simple connection example part II - TCP communication

SDK Version: 
M3
Last time I wrote about UDP connection. I got some comments about the problem, that UDP packets are not guaranteed to be delivered.
This time I’m going to show you the safer option, the TCP connection.
TCP is probably the most commonly used protocol, simply because it is used for so many applications such as HTTP, POP, SMTP, etc. TCP is a protocol which guarantees that the receiver will receive exactly what the sender sent - there will be no errors, it will be in the correct order, everything will work just fine.

TCP communication time diagram.

  1. try {
  2.         Socket s = new Socket("http://helloandroid.com&quot;,80);
  3. } catch (UnknownHostException e) {
  4.         // TODO Auto-generated catch block
  5.         e.printStackTrace();
  6. } catch (IOException e) {

How to run background jobs using threads

SDK Version: 
M3

Previously I wrote about, that slow operations must be runned in threads. Now I would like to present some example code, how to use threads in Android.

Under the android system an user interface element can only be accessed from the thread that created it (the main UI thread). Thats where handlers and messages come in.

The user interface defines a handler like below:

  1. Handler handler = new Handler() {
  2.   @Override
  3.   public void handleMessage(Message msg) {
  4.     int arg1=msg.arg1;
  5.     int arg2=msg.arg2;
  6.     MyClass myObject=(MyClass)msg.obj;
  7.     //do something in the user interface to display data from message
  8.   }
  9. }

Then the thread, which can not touch the user interface, sends a message to this handler instead.

The following example code, that implements file downloading from net, represents how I usually use the threads:

  1. public class Downloader {
  2.   Thread thread=null;
  3.   boolean interrupted=false;

Maintaining global Application state

SDK Version: 
M3

As a possible solutions mentioned in previous article Leaving an Android application the Application object can come handy. If you want to store data, global variables that needs to be accessed from everywhere in the application, from multiple Activities, in other words is you want to maintain a global "state" of the whole application the Application object can help.

For this we must make a class which extends the Android.app.Application class add our own methods to it, and define this class in the AndroidManifest.xml as below:

  1. An example for the Application class:
  2.  
  3. public class HelloApplication extends Application {
  4.         private int globalVariable=1;
  5.  
  6.         public int getGlobalVariable() {
  7.                 return globalVariable;
  8.         }

How to create a custom titlebar

SDK Version: 
M3

If you got sick and tired of the default style/behavior of the title bar in your apps, or just need something different, than here is a little snippet for You.

Speeding up android applications

SDK Version: 
M3

First of all, define what do we mean under "speed": in one hand its the time that the code needs to execute, on the other hand its the time the user needs to wait for the user interface. The two things can greatly differ, of course you must optimize the code performance, but the most important is what the user sees from it. Don't make the user wait, unless its necessary.

Do not pull back the ui thread
The very basic principle is to never run slow operations on the user interface thread! If you do this the interface will freeze until the operation is executed, which is not a nice user experience. If the execution takes too long the android system will detect it and offer the opportunity to the user to force close the frozen application:

Xml remote procedure calls on android

SDK Version: 
M3

Using web services on android phones, is pretty simple. For most popular services, there is a usable library available.
Here is a little snippet for using an android library, called android-xmlrpc.

Syndicate content