Posts

Multi Threading in Android

It is good practice to not do computation intensive work on the UI Thread. In fact starting from HoneyComb, you get a NetworkOnMainThread exception if you try and do any network processing on the main thread. So we should do background work in a new thread. One thing we need to consider is that only the UI thread can update the UI, so when our background thread wants to update the UI, we need a means to communicate between threads. Actually even your background thread can update the UI, but this is really dangerous and your app can crash! Android provides a lot of multi threading constructs for us. AsyncTask is by far the easiest way of multithreading in Android. When you create a new AsyncTask object the framework creates a new thread for you and provides methods to communicate with the UI. No threads, no handlers and no Executors (Because it uses these classes internally)! So why not use AsyncTask all the time? Well, because these are suited for background work that typically l...

Env vars on Ubuntu

To set them, export VAR=1 To check them, printenv To unset, unset VAR. On Ubuntu, you can set vim ~/.pam_environment and then write: JAVA_HOME=/usr/local/bin/jdk1.6.0_30 PATH DEFAULT=${PATH}:${JAVA_HOME}/bin Or, you can go to ~/ .bashrc and write: export JAVA_HOME="/usr/local/bin/jdk1.6.0_30" export PATH=$PATH:$JAVA_HOME/bin

Vim pasting

Vim keeps messing up your pastes. Type :set paste Pasting it now works. Once done, you can type :set nopaste Thanks to http://www.protocolostomy.com/2008/04/04/vim-messing-up-indentation-on-your-pasted-code/

High Res timer on Win using C++

Took this from http://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter #include double PCFreq = 0.0 ; __int64 CounterStart = 0 ; void StartCounter () { LARGE_INTEGER li ; if (! QueryPerformanceFrequency (& li )) cout << "QueryPerformanceFrequency failed!\n" ; PCFreq = double ( li . QuadPart )/ 1000.0 ; QueryPerformanceCounter (& li ); CounterStart = li . QuadPart ; } double GetCounter () { LARGE_INTEGER li ; QueryPerformanceCounter (& li ); return double ( li . QuadPart - CounterStart )/ PCFreq ; } int main () { StartCounter (); Sleep ( 1000 ); cout << GetCounter () << "\n" ; return 0 ; }

Diff

Do a 'git status' in the directory you are concerned with. This will give you the list of files that have changed. Then you can do:  'git diff filename.java > changes.patch' This creates the patch file!

Unable to update Android SDK on Windows?

Some things I had to try to get it to work: Close Eclipse and any other folder that had the android sdk folder opened. Go to /tools Right click on android.bat and select 'Run as Administrator' This should work and you should be able to update the SDK now. If not, well you are screwed...

Logging from native service in Android

To log to a text file from a native daemon process, push the empty file beforehand to /data. Next check that the owner of the process and the location where you are writing are the same.