Showing posts with label Utility. Show all posts
Showing posts with label Utility. Show all posts

Saturday, April 28, 2012

Utility Function Call, Email, Send SMS in android Using Intent

Generally while we making an android application then sometimes we need small piece of code like how to call a number on button click, how to send an email, how to send a sms. But problem is that it does not available at one place. So i am giving all possible Utility code here

1) How to call a number in android application

 Intent callIntent = new Intent(Intent.ACTION_CALL);  
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent);


2) How to send an email in android application

 Intent(android.content.Intent.ACTION_SENDTO);
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "testing email send.");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<b>this is html text in email body.</b>"));
startActivity(Intent.createChooser(emailIntent, "Email to Friend"));



3) How to send SMS in android application

 SmsManager sm = SmsManager.getDefault();
String number = "6508570720";
sm.sendTextMessage(number, null, "Test SMS Message", null, null);


Note : Never forget to mention for sending SMS <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

Now enjoy these Utility in your application and feel free to support me by commenting and giving feedback

How to pick an Email from contact in android

Picking email, Phone number, and other detail from android in built contact is very easy if you use ACTION_PICK.Action Pick activity syntax will be like


Intent intent1=new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent1,100);


Note : it need permission .so do not forget to mention it in manifest

<uses-permission android:name="android.permission.READ_CONTACTS"/>

It will start a default activity that will list all contact. On item select that activity will finish automatically and it will return result in OnActivityResult. default activity return complete Intent with all available information with particular select contact.



The main issue is now to handle Intent result in OnActivityResult. So now we will handle Intent



if(requestCode==100){
try{
if(resultCode==Activity.RESULT_OK){
Uri uri=data.getData();
String[] projection = new String[] {
ContactsContract.Contacts._ID,ontactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
};
Cursor emailCur=getContentResolver().query(uri,null, null, null,null);
emailCur.moveToFirst();
String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
Log.i("dddd",""+email+"djdjdj"+emailType);
emailCur.close();
}
catch(Exception e){
e.getCause();
}
}
}


                                    Download Sample