Tuesday, February 3, 2015

Fetch all Contacts from Contacts Content Provider in Android

Here I am going to explain how to fetch all contacts from Contacts content provider. As you already know content provider is used to share data between applications. Here we will get the data from contacts content provider to our application. First we have to create an object of ContentResolver.

 ContentResolver contentResolver = getContentResolver()  

Then query the content resolver using the Contacts URI.

 Cursor contactsCursor = resolver.query(Contacts.CONTENT_URI, null, null, null, null);  

After querying we will get the contacts ID. Using contacts ID, we can get the phone number,


 String id = contactsCursor.getString(contactsCursor.getColumnIndex(Contacts._ID));  
 // to get phone number of respective contact  
 Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = ?", new String[] { id }, null);  
 String phoneNumber = "";  
 if (phoneCursor.moveToFirst()) {  
      phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(Phone.NUMBER));  
 }  
 phoneCursor.close();  

Then to get the contacts name, contacts photo and email, use the below code

 String name = contactsCursor.getString(contactsCursor.getColumnIndex(Contacts.DISPLAY_NAME));  
 String photoUri = "";  
 photoUri = contactsCursor.getString(contactsCursor.getColumnIndex(Photo.PHOTO_URI));  
 // to get email id of respective contact  
 Cursor emailCursor = resolver.query(Email.CONTENT_URI, null, Email.CONTACT_ID + " = ?", new String[] { id }, null);  
 String email = "";  
 if (emailCursor.moveToFirst()) {  
      email = emailCursor.getString(emailCursor.getColumnIndex(Email.ADDRESS));  
 }  
 emailCursor.close();  

Here some tips for SQLITE query. To limit the records, in query() API for the last parameter first sort for an attribute, then limit the records using limit ,

Eg: Contacts._ID DESC LIMIT 10,10

Here contacts will be sorted in descending order by _ID and we will get 10 records from 10 to 20.

The new query looks like this,

 Cursor contactsCursor = resolver.query(Contacts.CONTENT_URI, null, null, null, Contacts._ID+" DESC LIMIT 10,10");  

We can get the full source code by clcicking HERE

The screenshots are here as follows,


Also by clicking on the selected contact, it will call to that particular contact, if it has a phone number.

Monday, February 2, 2015

Driving direction between two locations in Android

Here, I am explaining the driving direction between two locations in google map. It is an utility function. Here we are passing two locations to a method called "showMapFromLocation", first convert the location to corresponding latitude and longitude. Then using launch google map using intent. Here I just hard coded the package and launcher activity of google map. It is better to check the application (google map) is already there in the device using packagemanager.

The utility function is as follows,

 private void showMapFromLocation(String src, String dest) {  
           double srcLat = 0, srcLng = 0, destLat = 0, destLng = 0;  
           Geocoder geocoder = new Geocoder(this, Locale.getDefault());  
           try {  
                if (isOnline()) {  
                     List<Address> srcAddresses = geocoder.getFromLocationName(src,  
                               1);  
                     if (srcAddresses.size() > 0) {  
                          Address location = srcAddresses.get(0);  
                          srcLat = location.getLatitude();  
                          srcLng = location.getLongitude();  
                     }  
                     List<Address> destAddresses = geocoder.getFromLocationName(  
                               dest, 1);  
                     if (destAddresses.size() > 0) {  
                          Address location = destAddresses.get(0);  
                          destLat = location.getLatitude();  
                          destLng = location.getLongitude();  
                     }  
                     String desLocation = "&daddr=" + Double.toString(destLat) + ","  
                               + Double.toString(destLng);  
                     String currLocation = "saddr=" + Double.toString(srcLat) + ","  
                               + Double.toString(srcLng);  
                     // "d" means driving car, "w" means walking "r" means by bus  
                     Intent intent = new Intent(android.content.Intent.ACTION_VIEW,  
                               Uri.parse("http://maps.google.com/maps?" + currLocation  
                                         + desLocation + "&dirflg=d"));  
                     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK  
                               & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);  
                     intent.setClassName("com.google.android.apps.maps",  
                               "com.google.android.maps.MapsActivity");  
                     startActivity(intent);  
                }  
           } catch (IOException e) {  
                Log.e(TAG, "Error when showing google map directions, E: " + e);  
           } catch (Exception e) {  
                Log.e(TAG, "Error when showing google map directions, E: " + e);  
           }  
      }  

We can select which mode of driving direction we need according to the flag "dirflg".
    - dirflg = d, means driving by car
    - dirflg = w, means by walking and
    - dirflg = r, means by bus

The screenshots are like,

One last thing is we have to add google play services library to show the google maps.

Androidians