A Guide to Mobile and Web Technology(LAMP)

Posts tagged ‘mobile’

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
Advertisements

Listener for Incoming SMS in Android

The following code block allows you to listen for all the incoming sms in Android

public class SmsReceiver  extends BroadcastReceiver {
	
    private Context mContext;
    private Bundle mBundle;
    
    private String smsBodyStr = "", phoneNoStr = "";
    private long smsDatTime = System.currentTimeMillis();
    
    public void onReceive(Context context, Intent intent) {
		try{
			mContext = context;
			mBundle = intent.getExtras();  
			
		    if (mBundle != null){
		    	getSMSDetails();
		    }
		    else
		    	Log.e("Info","Bundle is Empty!");
		}
		catch(Exception sgh){
			Log.e("ERROR", "Error in Init : "+sgh.toString());
		}
	}//fn onReceive

	private void getSMSDetails(){	     
	    SmsMessage[] msgs = null;
		try{
			Object[] pdus = (Object[]) mBundle.get("pdus");
			if(pdus != null){
				msgs = new SmsMessage[pdus.length];
				Log.e("Info","pdus length : "+pdus.length);
				for(int k=0; k<msgs.length; k++){
					msgs[k] = SmsMessage.createFromPdu((byte[])pdus[k]);  
					
					Log.e("Info","getDisplayMessageBody : "+msgs[k].getDisplayMessageBody());
					Log.e("Info","getDisplayOriginatingAddress : "+msgs[k].getDisplayOriginatingAddress());
					Log.e("Info","getMessageBody : "+msgs[k].getMessageBody());
					Log.e("Info","getOriginatingAddress : "+msgs[k].getOriginatingAddress());
					Log.e("Info","getProtocolIdentifier : "+msgs[k].getProtocolIdentifier());
					Log.e("Info","getStatus : "+msgs[k].getStatus());
					Log.e("Info","getStatusOnIcc : "+msgs[k].getStatusOnIcc());
					Log.e("Info","getStatusOnSim : "+msgs[k].getStatusOnSim());
					
					smsBodyStr = msgs[k].getMessageBody().trim();
					phoneNoStr = msgs[k].getOriginatingAddress().trim();
					smsDatTime = msgs[k].getTimestampMillis();
					
					Log.e("Info","SMS Content : "+smsBodyStr);
					Log.e("Info","SMS Phone No : "+phoneNoStr);
					Log.e("Info","SMS Time : "+smsDatTime);
				}
			}
		}
		catch(Exception sfgh){
			Log.e("ERROR", "Error in getSMSDetails : "+sfgh.toString());
		}
	}//fn getSMSDetails
	
}//End of class SmsReceiver