6/21/2016
AndroidSendingSMS
AndroidSendingSMS
Advertisements
PreviousPage
NextPage
InAndroid,youcanuseSmsManagerAPIordevicesBuiltinSMSapplicationtosendSMS's.
Inthistutorial,weshowsyoutwobasicexamplestosendSMSmessage
SmsManagerAPI
SmsManagersmsManager=[Link]();
[Link]("phoneNo",null,"smsmessage",null,null
BuiltinSMSapplication
IntentsendIntent=newIntent(Intent.ACTION_VIEW);
[Link]("sms_body","defaultcontent");
[Link]("[Link]/mmssms");
startActivity(sendIntent);
Ofcourse,bothneedSEND_SMSpermission.
<usespermissionandroid:name="[Link].SEND_SMS"/>
Apart from the above method, there are few other important functions available in
[Link]
[Link].
Method&Description
ArrayList<String>divideMessage(Stringtext)
This method divides a message text into several
fragments, none bigger than the maximum SMS
messagesize.
2
staticSmsManagergetDefault()
This method is used to get the default instance of
theSmsManager
3
voidsendDataMessage(StringdestinationAddress,
StringscAddress,shortdestinationPort,byte[]
data,PendingIntentsentIntent,PendingIntent
deliveryIntent)
ThismethodisusedtosendadatabasedSMStoa
specificapplicationport.
4
voidsendMultipartTextMessage(String
destinationAddress,StringscAddress,
[Link]
1/10
6/21/2016
AndroidSendingSMS
ArrayList<String>parts,
ArrayList<PendingIntent>sentIntents,
ArrayList<PendingIntent>deliveryIntents)
SendamultiparttextbasedSMS.
5
voidsendTextMessage(StringdestinationAddress,
StringscAddress,Stringtext,PendingIntent
sentIntent,PendingIntentdeliveryIntent)
SendatextbasedSMS.
Example
FollowingexampleshowsyouinpracticalhowtouseSmsManagerobjecttosendanSMSto
thegivenmobilenumber.
Toexperimentwiththisexample,youwillneedactualMobiledeviceequippedwith
latestAndroidOS,otherwiseyouwillhavetostrugglewithemulatorwhichmaynot
work.
Step
Description
YouwilluseAndroidStudioIDEtocreateanAndroid
applicationandnameitastutorialspointundera
[Link]
project,makesureyouTargetSDKandCompileWithat
thelatestversionofAndroidSDKtousehigherlevelsof
APIs.
Modifysrc/[Link]
takecareofsendingemail.
ModifylayoutXMLfileres/layout/activity_main.xmladd
anyGUIcomponentifrequired.I'maddingasimpleGUI
totakemobilenumberandSMStexttobesentanda
simplebuttontosendSMS.
Noneedtodefinedefaultstringconstantsat
res/values/[Link]
defaultconstants.
[Link]
RuntheapplicationtolaunchAndroidemulatorandverify
theresultofthechangesdoneintheapplication.
Following
is
the
content
of
the
modified
main
activity
filesrc/[Link]/[Link].
[Link];
[Link]
2/10
6/21/2016
AndroidSendingSMS
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
publicclassMainActivityextendsActivity{
ButtonsendBtn;
EditTexttxtphoneNo;
EditTexttxtMessage;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
[Link](savedInstanceState);
setContentView([Link].activity_main);
sendBtn=(Button)findViewById([Link]);
txtphoneNo=(EditText)findViewById([Link]);
txtMessage=(EditText)findViewById([Link].editText2);
[Link]([Link](){
publicvoidonClick(Viewview){
sendSMSMessage();
}
});
}
protectedvoidsendSMSMessage(){
Log.i("SendSMS","");
StringphoneNo=[Link]().toString();
Stringmessage=[Link]().toString();
try{
SmsManagersmsManager=[Link]();
[Link](phoneNo,null,message,null,null
[Link](getApplicationContext(),"SMSsent.",Toast.
}
catch(Exceptione){
[Link](getApplicationContext(),"SMSfaild,pleasetryagain."
[Link]();
}
}
@Override
publicbooleanonCreateOptionsMenu(Menumenu){
//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
getMenuInflater().inflate([Link],menu);
returntrue;
}
}
Followingwillbethecontentofres/layout/activity_main.xmlfile:
<RelativeLayoutxmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
[Link]
3/10
6/21/2016
AndroidSendingSMS
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SendingSMSExample"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorialspoint"
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_below="@+id/textView1"
android:layout_alignRight="@+id/imageButton"
android:layout_alignEnd="@+id/imageButton"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="EnterPhoneNumber"
android:phoneNumber="true"
android:textColorHint="@color/abc_primary_text_material_dark"
android:layout_below="@+id/imageButton"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText"
android:textColorHint="@color/abc_primary_text_material_dark"
android:layout_alignRight="@+id/imageButton"
android:layout_alignEnd="@+id/imageButton"
android:hint="EnterSMS"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SendSms"
android:id="@+id/btnSendSMS"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"/>
</RelativeLayout>
[Link]
4/10
6/21/2016
AndroidSendingSMS
Followingwillbethecontentofres/values/[Link]
<?xmlversion="1.0"encoding="utf8"?>
<resources>
<stringname="app_name">tutorialspoint</string>
<stringname="action_settings">Settings</string>
</resources>
[Link]:
<?xmlversion="1.0"encoding="utf8"?>
<manifestxmlns:android="[Link]
package="[Link]"
android:versionCode="1"
android:versionName="1.0">
<usessdk
android:minSdkVersion="8"
android:targetSdkVersion="22"/>
<usespermissionandroid:name="[Link].SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="[Link]"
android:label="@string/app_name">
<intentfilter>
<actionandroid:name="[Link]"/>
<categoryandroid:name="[Link]"
</intentfilter>
</activity>
</application>
</manifest>
Let'strytorunyourtutorialspointapplication. I assume you have connected your actual
[Link],openoneof
yourproject'sactivityfilesandclickRun
icon from the toolbar. Before starting your application, Android studio installer will display
followingwindowtoselectanoptionwhereyouwanttorunyourAndroidapplication.
[Link]
5/10
6/21/2016
AndroidSendingSMS
Nowyoucanenteradesiredmobilenumberandatextmessagetobesentonthatnumber.
[Link]/CDMAconnection
isworkingfinetodeliveryourSMStoitsrecipient.
YoucantakeanumberofSMSseparatedbycommaandtheninsideyourprogramyouwill
havetoparsethemintoanarraystringandfinallyyoucanusealooptosendmessagetoall
the given numbers. That's how you can write your own SMS client. Next section will show
youhowtouseexistingSMSclienttosendSMS.
UsingBuiltinIntenttosendSMS
YoucanuseAndroidIntenttosendSMSbycallingbuiltinSMSfunctionalityoftheAndroid.
FollowingsectionexplainsdifferentpartsofourIntentobjectrequiredtosendanSMS.
IntentObjectActiontosendSMS
YouwilluseACTION_VIEWactiontolaunchanSMSclientinstalledonyourAndroiddevice.
FollowingissimplesyntaxtocreateanintentwithACTION_VIEWaction
IntentsmsIntent=newIntent(Intent.ACTION_VIEW);
IntentObjectData/TypetosendSMS
TosendanSMSyouneedtospecifysmsto:asURIusingsetData()methodanddatatype
[Link]/mmssmsusingsetType()methodasfollows
[Link]
6/10
6/21/2016
AndroidSendingSMS
[Link]([Link]("smsto:"));
[Link]("[Link]/mmssms");
IntentObjectExtratosendSMS
Android has builtin support to add phone number and text message to send an SMS as
follows
[Link]("address",newString("0123456789;3393993300"));
[Link]("sms_body","TestSMStoAngilla");
Hereaddressandsms_bodyarecasesensitiveandshouldbespecifiedinsmall
[Link]
bysemicolon().
Example
FollowingexampleshowsyouinpracticalhowtouseIntentobjecttolaunchSMSclientto
sendanSMStothegivenrecipients.
Toexperimentwiththisexample,youwillneedactualMobiledeviceequippedwith
latestAndroidOS,otherwiseyouwillhavetostrugglewithemulatorwhichmaynot
work.
Step
Description
YouwilluseAndroidstudioIDEtocreateanAndroid
applicationandnameitastutorialspointundera
[Link]
project,makesureyouTargetSDKandCompileWithat
thelatestversionofAndroidSDKtousehigherlevelsof
APIs.
Modifysrc/[Link]
takecareofsendingSMS.
ModifylayoutXMLfileres/layout/activity_main.xmladd
anyGUIcomponentifrequired.I'maddingasimple
buttontolaunchSMSClient.
[Link]
careofdefaultconstants.
[Link]
RuntheapplicationtolaunchAndroidemulatorandverify
theresultofthechangesdoneintheapplication.
Following
is
the
content
[Link]
of
the
modified
main
activity
7/10
6/21/2016
AndroidSendingSMS
filesrc/[Link]/[Link].
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
publicclassMainActivityextendsActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
[Link](savedInstanceState);
setContentView([Link].activity_main);
ButtonstartBtn=(Button)findViewById([Link]);
[Link]([Link](){
publicvoidonClick(Viewview){
sendSMS();
}
});
}
protectedvoidsendSMS(){
Log.i("SendSMS","");
IntentsmsIntent=newIntent(Intent.ACTION_VIEW);
[Link]([Link]("smsto:"));
[Link]("[Link]/mmssms");
[Link]("address",newString("01234"));
[Link]("sms_body","Test");
try{
startActivity(smsIntent);
finish();
Log.i("FinishedsendingSMS...","");
}
catch([Link]){
[Link]([Link],
"SMSfaild,pleasetryagainlater.",Toast.LENGTH_SHORT).show
}
}
@Override
publicbooleanonCreateOptionsMenu(Menumenu){
//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
getMenuInflater().inflate([Link],menu);
returntrue;
}
}
Followingwillbethecontentofres/layout/activity_main.xmlfile
<RelativeLayoutxmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
[Link]
8/10
6/21/2016
AndroidSendingSMS
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DragandDropExample"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TutorialsPoint"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:textSize="30dp"
android:textColor="#ff14be3c"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_marginTop="48dp"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ComposeSMS"
android:id="@+id/button"
android:layout_below="@+id/imageView"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2"
android:layout_marginTop="54dp"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView"/>
</RelativeLayout>
Followingwillbethecontentofres/values/[Link]
<?xmlversion="1.0"encoding="utf8"?>
<resources>
<stringname="app_name">tutorialspoint</string>
<stringname="action_settings">Settings</string>
</resources>
[Link]
<?xmlversion="1.0"encoding="utf8"?>
<manifestxmlns:android="[Link]
package="[Link]"
android:versionCode="1"
[Link]
9/10
6/21/2016
AndroidSendingSMS
android:versionName="1.0">
<usessdk
android:minSdkVersion="8"
android:targetSdkVersion="22"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name
[Link]
10/10