Tuesday, November 18, 2014

Open Contacts using Intent and get Contact in onActivityResult()

Here, I am launching contacts from my app using a button click. I am opening contacts which have phone numbers. Here we are launching contacts using startActivityForResult, so while returning back to the app, app will get the selected contact info in onActivityResult() of Activity class.

The code snippet is as,

 ((Button) findViewById(R.id.btn_openContacts)).setOnClickListener(new View.OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                     Intent i = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);  
                     i.setType(Phone.CONTENT_TYPE);  
                     startActivityForResult(i, PICK_CONTACT);  
                }  
 });  
onActivityResult() will be look like this,
 @Override  
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
           super.onActivityResult(requestCode, resultCode, data);  
           Log.i(TAG, "onActivityResult()");  
           if (requestCode == PICK_CONTACT) {  
                if (resultCode == Activity.RESULT_OK) {  
                     Uri contactsData = data.getData();  
                     CursorLoader loader = new CursorLoader(this, contactsData, null, null, null, null);  
                     Cursor c = loader.loadInBackground();  
                     if (c.moveToFirst()) {  
                          Log.i(TAG, "Contacts ID: " + c.getString(c.getColumnIndex(Contacts._ID)));  
                          Log.i(TAG, "Contacts Name: " + c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME)));  
                          Log.i(TAG, "Contacts Phone Number: " + c.getString(c.getColumnIndex(Phone.NUMBER)));  
                          Log.i(TAG, "Contacts Photo Uri: " + c.getString(c.getColumnIndex(Photo.PHOTO_URI)));  
                     }  
                }  
           }  
      }  

Here I am just getting the details and getting in the Logcat. Just to verify I am getting the contact details

The screenshots are,

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.

Androidians