Tuesday, April 8, 2014

CalledFromWrongThreadException in Android

I got CalledFromWrongThreadException when I tried to update UI from another thread. I have two threads in my app, one our main UI thread and another for network operations. So when I tried to update UI from "network thread", I got the exception CalledFromWrongThreadException.

Solution:
The solution for this is update the UI in UI thread. For this, just put the update UI statements inside runOnUiThread block. First get the activity context, and call runOnUiThread(). Like,
runOnUIThread(new Runnable() {
public void run() {
...
}
});
Happy Coding

Sunday, March 9, 2014

Recap: A look back for Android, from late 2008 to early 2014

I was not active in blogging for a while, and after a long gap, posting a new one. All my previous posts are from my app development experience in Android. This one is a little different. I like to tell about brief history of Android from my experience and updates

Android changed a lot from the initial days. Late 2008 and early 2009's, where Android phones are just entering into the smartphone market and first phone was from T-Mobile called G1. During that time, testing of the apps are mainly on emulator, so when I first tested my app in an real Android phone, I was so excited. G1 is almost like what we see in an emulator. It was Android SDK v 1.0. Now, jelly bean and the latest kitkat arrived, which are improved a lot, also in the process, Android evolved as a strong mobile OS as well as matured a lot in terms of security, user experience and added more features.

In the global smartphone market, 80% share belongs to Android. In five years from 0 to 80% share is a big achievement. Of this, 80% of Android phones are Android version ICS (API level 14 and above). In this majority of the share goes to Samsung. Samsung share in Android phones is about 63%. That means Out of 80 Android phones, 50 are from Samsung.

Tablets

Now Android is capturing Tablet market also, In 2013, Android takes 62% market share of tablet as Apple's share declines.

Note:
The numbers in this post are got from various sites. I did my best to get the numbers accurate and updated.

Friday, June 7, 2013

Open Android market (Google Play Store) using Intent

To open google play store (Android market) using intent, just use the code snippet
 

String appName = "";  
Uri uri = Uri.parse("market://details?id=" + appName);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (isAvaialable(intent, mContext)) {
startActivity(intent);  
} else {
Uri uri = Uri.parse("http://play.google.com/store/apps/details?id="+appName);
intent.setData(uri);
startActivity(intent); 
}  

public static boolean isAvailable(Context ctx, Intent intent) {
   final PackageManager mgr = ctx.getPackageManager();
   List list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
   return list.size() > 0;
}
isAvailable() is an utility method that I got from http://www.grokkingandroid.com/checking-intent-availability/

Uniquely Identify a device in Android

For many of the application we may require to uniquely Identify android device. The best way to get the unique device Id is through Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID); according to the "android developers blog".

Wednesday, May 22, 2013

After updating to ADT 22, no more R.java files

I recently came across this problem that after updating the ADT to 22, none of my apps are building. Android can't create the R.java file. The solution for this is, open "Android SDK Manager". Download "Android SDK build tools". Then for safety, restart your eclipse. That's it. Back to normal. All apps start building.

Wednesday, April 3, 2013

Dropbox client for Android

In one of my projects, I need to create a client in Android for Dropbox. So I like to share my experience in creating the Dropbox client. Dropbox has an Android SDK. So developers can just call the API and can use. When connecting the app with any cloud say, the authentication part is the tricky and some what complicated one. Once app establishes a connection to the cloud, then getting resources (say, files) is just a cakewalk. First I will just list down the steps for creating the dropbox client in Android.

1. First create an account in "Dropbox"

2. Then in developers page, go to the tab "My Apps". Click on the "Create an App". A dialog appears, there we specify our App name as well as the access level, either "full access" or "app folder" (means only to a specific folder in your account). We can select any one according to our choice.



3. After creating the app in dropbox, we will get the "App Key" & "App Secret". We need to use this "App Key" & "App Secret" in our application for authenticating our app with Dropbox cloud.

4. Open eclipse, create an Android app with application name same as what we provided in Dropbox page.

5. In the manifest file of the app, we need to put
 <activity   
 android:name="com.dropbox.client2.android.AuthActivity" android:launchMode="singleTask"   
 android:configChanges="orientation|keyboard">  
   <intent-filter>  
      <!-- Change this to be db- followed by our app key -->  
       <data android:scheme="db-our app key" />  
       <action android:name="android.intent.action.VIEW" />          
       <category android:name="android.intent.category.BROWSABLE"/>  
       <category android:name="android.intent.category.DEFAULT" />   
   </intent-filter>  
 </activity>  
This is for authenticating our app. For authenticating app will open browser with Dropbox login page, there we need to enter our Dropbox username & password. Dropbox will also ask for accessing Dropbox resources with this app. If we allow then only the android app can access Dropbox resources.

5. After allowing our app to access Dropbox resources, control will return to the application. App will return to the activtiy where it is called for authhentication. In that Activtiy in onRresume(), developer need to check whether the authentication with Dropbox is successful. If successful, app can take the desired action. Here I am just displaying all the resources in the account.

6. So app moves to the next activity named "ListDropboxActivity", where app will fetch all resources from the Dropbox and display the names of the resources as a listView. On Clicking on the resources names, app will open the resources, whether, it is a text file(.txt), or some images like .jpg, .png etc. This is all about the Dropbox client app.

You can get the full source code from here

Saturday, September 15, 2012

NullPointerException when itemizedOverlay doesn't contain any Overlay item

Yesterday when I was working in Android, I got a strange exception. I am adding an ItemizedOverlay to google map. I am adding OverlayItem to the ItemizedOverlay when one condition is true. But sometimes, this condition is false and not adding any OverlayItem to the ItemizedOverlay. But still adding the ItemizedOverlay to google maps. At this time, I am getting null pointer exception. First I didn't get what is going wrong. Then after some googling, I came to know we can't add ItemizedOverlay to google map when there is no OverlayItem in the Overlay. So be careful when adding empty ItemizedOverlay to google maps. I was working in API level 10. I am not sure whether this problem is in future versions.

Androidians