A Guide to Mobile and Web Technology(LAMP)

Posts tagged ‘search’

How to search for a contact using phone number in Android

The following code allows to retrieve the contact details using phone number

String contactId = "";
String contactName = "";

private void retrieveContactRecord(String phoneNo) {
	try{
		Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNo));
		String[] projection = new String[] { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME };
		String selection = null;
		String[] selectionArgs = null;
		String sortOrder = ContactsContract.PhoneLookup.DISPLAY_NAME+ " COLLATE LOCALIZED ASC";
		ContentResolver cr = mContext.getContentResolver();
		if(cr != null){
			Cursor resultCur = cr.query(uri, projection, selection, selectionArgs, sortOrder);
			if(resultCur != null){
				while (resultCur.moveToNext()) {
					contactId = resultCur.getString(resultCur.getColumnIndex(ContactsContract.PhoneLookup._ID));
					contactName = resultCur.getString(resultCur.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
					Log.e("Info","Contact Id : "+contactId);
					Log.e("Info","Contact Display Name : "+contactName);
					break;
				}
				resultCur.close();
			}
		}
	}
	catch(Exception sfg){
		Log.e("Error", "Error in loadContactRecord : "+sfg.toString());
	}
}//fn retrieveContactRecord

Google Search text code using php

The following lines of code will provide search results using the google search API.

//search text to be passes
	$search_text = "katharnavas";
	$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=".$search_text;

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$results = curl_exec($ch);
	curl_close($ch);

	// The results will be in json encoded format so do a decode
	$json = json_decode($results);
	print_r($json);