One thing which can be extremely annoying for developers is Eclipse. It can be a giant mess to run, and it doesn’t run well straight off the bat for users of Ubuntu 12.04. I share some of my tips to make it work really well for you.
Tip #1: Use the Oracle Java VM 7. It’s amazing how much different this is compared with Version 6 of either the official, or OpenJDK versions.
In android it is highly important to use transactions when working with databases.
First, in android database operations - especially writing - are very slow. Batching them into transactions will make them much faster.
Second, the database remains consistent under any circumstances. The database system makes sure to all the operations in a transaction take effect, or on error, rollback all of them.
If you are used to other platforms like PHP+MySQL where the code usually runs on a powerful server, witch is not likely to stop execution "unexpectedly", you can be surprised how much it affects the performance in android.
The android system can kill apps/threads/activities and so interrupt database usage, the battery can discharge or can be removed etc.
The implementation is very simple, using 3 methods in the SQLiteDatabase class:
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:
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: