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.
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.
try{
Socket s =newSocket("http://helloandroid.com",80);
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:
Handler handler =new Handler(){
@Override
publicvoid handleMessage(Message msg){
int arg1=msg.arg1;
int arg2=msg.arg2;
MyClass myObject=(MyClass)msg.obj;
//do something in the user interface to display data from message
}
}
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:
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/
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:
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.
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.
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:
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.
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.