Tuesday, April 17, 2012

Trouble with Android's Looper/Handler during facebook integration

I was trying to integrate Facebook in my android application but a problem haunted me for three days. In fact, it's still haunting but I kept it aside as it is happening only in few devices.

The issue comes when I try to authorize the application to facebook by calling facebook.authroize(); It is giving me the following exception:

Caused by: java.lang.RuntimeException: Can't create handler inside thread 
that has not called Looper.prepare()
02-15 22:52:43.483: ERROR/AndroidRuntime(5370): 
at android.os.Handler.<init>(Handler.java:121)
...
...
...

Adding Looper.prepare() and the related code should not be required because it is already called in the main thread before onCreate() in my Activity class. The main thread is somehow not finding the looper when it is sending this task to the handler.

I have added that Looper related code anyway which only lead to other problems like application freeze and some error messages.

I was stuck on this for 3 days only to find it working on some other device. So, the lesson learnt is, an application working in one device need not be working in another device and vice-versa. So, test the application in multiple devices, as many as possible. Also, don't block yourself for too long if you're stuck at some issue and blocked for 3 DAYS. It may be working fine in another device. Proceed forward, finish the app and come back to that error later.

If anyone came across same/similar issue and have found a solution, please respond. It is deeply appreciated.

Please comment if you like the post or in case, you have any queries.

Help me learn, Help you learn!

KVK








Facebook Integration in Android application

When you are trying to integrate Facebook to add that social layer flavour to your android application, I'm more than sure that you would have followed instructions from here - http://developers.facebook.com/docs/mobile/android/build/

You should have come across  a point where you need to generate a hashed signature of the android application that needs FB integration and give it to FB during registration. You might have used the following:

keytool -exportcert -alias androiddebugkey -keystore 
<PATH TO YOUR APPLICATION KEYSTORE FILE> | openssl sha1 -binary 
| openssl base64

 
It will generate a signature and you will add it in the facebook registration page. But the thing is, it may not be the actual value that Facebook expects you to mention when you make a call to its API. It may give an error like "invalid_key" or "Facebook doesn't recognize your Android key hash". When you come across this error, then read the complete message. If you are not logging any error, then do log it. THE ERROR MESSAGE ITSELF CARRIES THE KEY. Use that key for registation (edit the details) instead of the one that is generated in the above 'keytool' command. It will resolve your issue.

Please comment if you like the post or in case, you have any queries.

Help me learn, Help you learn

KVK

Thursday, April 12, 2012

C2DM issue

Android C2DM:

Cloud to Device Messaging is the latest technique (introduced in Android 2.2) that people are starting to use in their applications to have the real time or latest information. It has advantages not just like reducing the load on the application by preventing it from periodically polling the server but also from a mobile device point of view i.e, reduce the battery suck-up which is very important for an application. Anyway, you might have gone through various tutorials on how to set up C2DM in the application etc and so, I don't see a point putting them down here. But I can still give the highest level explanation.
  1. App from device A wants to send a message M to the app in device B.
  2. A sends the message M to application server.
  3. Application server sends another tiny message m to C2DM server (nothing but a google server) giving the destination details (by giving registration id of B)
  4. C2DM server in turn sends the message m to B (message reaches specific application in that specific device). Android wakes up the corresponding application.
  5. Application must know how to handle the message. Basically it should create a Status Bar Notification (SBN). 
  6. When user taps on the SBN, the application will connect to the server and get the actual message M.

The point of this post is to clarify (if anyone required any) about a misconception while handling C2DM's minimum API level requirement. Always check the API level running in the device but not the minSdkVersion mentioned in AndroidManifest.xml and then handle take decision whether to add C2DM in the application or not. That is, if the API level running in the device is >= Build.VERSION_CODES.FROYO, then only enable C2DM, otherwise don't.


Please comment if there are any corrections or you have any queries about C2DM.

Monday, April 2, 2012

Importance of TRIE datastructure

Do you have a website with search box in it? Are you looking how to achieve auto completion while entering text in the box. Then you need to checkout a data structure called TRIE. This may already be very old concept but it definitely is in demand now-a-days with lot of websites launching everyday.
This is one side of the face. TRIE can also be used and indeed, extensively used for maintaining dictionary, phone book etc. It also beats other data structures BST, Hash Table in regular operations (lookup, insert, delete).

Here are the relevant links:

About TRIE: http://en.wikipedia.org/wiki/Trie

TRIE implementation in Java:

http://exceptional-code.blogspot.in/2011/07/coding-up-trie-prefix-tree.html
http://www.technicalypto.com/2010/04/trie-in-java.html
http://www.technicalypto.com/2010/04/trie-data-structure-part-2-node-and.html


May this post be useful to you!