Sunday, June 23, 2013

Android:send message programatically

To send a message programatically, use

public void sendSMS() {
    String phoneNumber = "0123456789";
    String message = "Hello World!";

    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNumber, null, message, null, null);
}

The above method will support only up to 160 characters.

Include the following permission in your AndroidManifest.xml file
<uses-permission android:name="android.permission.SEND_SMS" />

Import the package
import android.telephony.SmsManager;

To send long sms use:-

String phoneNumber = "0123456789";
    String message = "Hello World! Now we are going to demonstrate " +
            "how to send a message with more than 160 characters from your Android application.";

    SmsManager smsManager = SmsManager.getDefault();
    ArrayList<String> parts = smsManager.divideMessage(message);
    smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null);

To send sms using built-in SMS Application:-

        Intent smsIntent = new Intent(Intent.ACTION_VIEW);
        smsIntent.putExtra("sms_body", "Hello World!");
        smsIntent.putExtra("address", "0123456789");
        smsIntent.setType("vnd.android-dir/mms-sms");


        startActivity(smsIntent);

 If you wish to set multiple recipients use semi-colon ';' as the separator in the string passed as the address