Tuesday, August 4, 2015

[Android] Sending SMS Message

Android provided a class called SmsManager that can be used to send SMS message. Below is a sample code to show how to use the class to send SMS.



import android.telephony.SmsManager;



private boolean send(String number, String message) {
 boolean b = true;
 Log.i(MainActivity.class.getName(),">>Sending sms ::: " + number + " with message ::: " + message);
 try {
  SmsManager smsManager = SmsManager.getDefault();
  smsManager.sendTextMessage(number, null, message, null, null);
  Log.i(MainActivity.class.getName(),">>Sent - OK!");
 }catch (Exception e) {
  b = false;
  e.printStackTrace();
 }
 return b;
}//End method


Below lines of code should be added to AndroidManifest.xml.


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


No comments:

Post a Comment