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",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;

Android jokes

- Two Android phone are walking in the desert. Which of them is 2.2?
- The one who called "Froyo".

How many Android user does it take to change a light bulb?
One. He puts the bulb in and lets the world revolve around him.

Question: Definition of an upgrade?
Answer: Take old bugs out, put new ones in.

Chuck Norris runs Android on his I-Phone.

- What does a slow & lazy Android phone say?
- Force Close.

- Do you want to hear a dirty joke?
- Ok
- A white HTC Hero fell in the mud.

- Did you Nexus One help you with your homework?
- Student: No, it did it all by itself

Simple UDP communication example

SDK Version: 
M3

Today I’m going to show you how to create a really easy and simple UDP message sender and receiver.


Udp communication time diagram.

The User Datagram Protocol (UDP) is one of the core members of the Internet Protocol Suite, the set of network protocols used for the Internet. With UDP, computer applications can send messages, in this case referred to as datagrams, to other hosts on an Internet Protocol (IP) network without requiring prior communications to set up special transmission channels or data paths. /wiki/

First of all let’s see the client side:

  1. String message="Hello Android!";
  2. int server_port = 12345;
  3. InetAddress local = InetAddress.getByName("192.168.1.102");
  4. int msg_length=message.length();
  5. byte[] message = message.getBytes();
  6. DatagramPacket p = new DatagramPacket(message, msg_length,local,server_port);

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.

Wallpaper tutorial - Part 2

SDK Version: 
M3

This article was requested by some of our community members. It is based on the Android Gallery, ImageView Example tutorial by Sasikumar (Part 1 is here). I extended his example with a new function. If you long click on the big image, an alert dialog show up and ask “Do you want to use this image as a wallpaper?". If you click yes, the actually image will be your new wallpaper.

I only changed the Activity class:

  1. package org.androidpeople.gallery;
  2.  
  3. import java.io.IOException;
  4.  
  5. import android.app.Activity;
  6. import android.app.AlertDialog;
  7. import android.content.Context;
  8. import android.content.DialogInterface;
  9. import android.content.res.TypedArray;
  10. import android.graphics.Bitmap;

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:

Move your app update: we got our nexus one!

Last week, our award for 4th Place in the Move your app challange, a Nexus one has finally arrived. The shipping took 2 months, and we were surprised, that the phone came in without any damage.
nexus
I won't go into detail about unboxing, and reviewing all the little details, the gadget sites already tore the nexus one apart months ago anyway.
Lets take a look at it, from the developers point of view.

Syndicate content