Showing posts from February, 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(P…

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 …