Send to all sms android

Android — Sending SMS

In Android, you can use SmsManager API or devices Built-in SMS application to send SMS’s. In this tutorial, we shows you two basic examples to send SMS message −

Built-in SMS application

Of course, both need SEND_SMS permission.

Apart from the above method, there are few other important functions available in SmsManager class. These methods are listed below −

ArrayList divideMessage(String text)

This method divides a message text into several fragments, none bigger than the maximum SMS message size.

static SmsManager getDefault()

This method is used to get the default instance of the SmsManager

void sendDataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent)

This method is used to send a data based SMS to a specific application port.

void sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList parts, ArrayList

Send a multi-part text based SMS.

void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

Send a text based SMS.

Example

Following example shows you in practical how to use SmsManager object to send an SMS to the given mobile number.

To experiment with this example, you will need actual Mobile device equipped with latest Android OS, otherwise you will have to struggle with emulator which may not work.

Sr.No. Method & Description
1
Step Description
1 You will use Android Studio IDE to create an Android application and name it as tutorialspoint under a package com.example.tutorialspoint.
2 Modify src/MainActivity.java file and add required code to take care of sending sms.
3 Modify layout XML file res/layout/activity_main.xml add any GUI component if required. I’m adding a simple GUI to take mobile number and SMS text to be sent and a simple button to send SMS.
4 No need to define default string constants at res/values/strings.xml. Android studio takes care of default constants.
5 Modify AndroidManifest.xml as shown below
6 Run the application to launch Android emulator and verify the result of the changes done in the application.

Following is the content of the modified main activity file src/com.example.tutorialspoint/MainActivity.java.

Following will be the content of res/layout/activity_main.xml file −

Here abc indicates about tutorialspoint logo

Following will be the content of res/values/strings.xml to define two new constants −

Following is the default content of AndroidManifest.xml

Let’s try to run your tutorialspoint application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Android studio, open one of your project’s activity files and click Run icon from the toolbar. Before starting your application, Android studio installer will display following window to select an option where you want to run your Android application.

Now you can enter a desired mobile number and a text message to be sent on that number. Finally click on Send SMS button to send your SMS. Make sure your GSM/CDMA connection is working fine to deliver your SMS to its recipient.

You can take a number of SMS separated by comma and then inside your program you will have to parse them into an array string and finally you can use a loop to send message to all the given numbers. That’s how you can write your own SMS client. Next section will show you how to use existing SMS client to send SMS.

Using Built-in Intent to send SMS

You can use Android Intent to send SMS by calling built-in SMS functionality of the Android. Following section explains different parts of our Intent object required to send an SMS.

Intent Object — Action to send SMS

You will use ACTION_VIEW action to launch an SMS client installed on your Android device. Following is simple syntax to create an intent with ACTION_VIEW action.

Intent Object — Data/Type to send SMS

To send an SMS you need to specify smsto: as URI using setData() method and data type will be to vnd.android-dir/mms-sms using setType() method as follows −

Intent Object — Extra to send SMS

Android has built-in support to add phone number and text message to send an SMS as follows −

Here address and sms_body are case sensitive and should be specified in small characters only. You can specify more than one number in single string but separated by semi-colon (;).

Example

Following example shows you in practical how to use Intent object to launch SMS client to send an SMS to the given recipients.

To experiment with this example, you will need actual Mobile device equipped with latest Android OS, otherwise you will have to struggle with emulator which may not work.

Step Description
1 You will use Android studio IDE to create an Android application and name it as tutorialspoint under a package com.example.tutorialspoint.
2 Modify src/MainActivity.java file and add required code to take care of sending SMS.
3 Modify layout XML file res/layout/activity_main.xml add any GUI component if required. I’m adding a simple button to launch SMS Client.
4 No need to define default constants.Android studio takes care of default constants.
5 Modify AndroidManifest.xml as shown below
6 Run the application to launch Android emulator and verify the result of the changes done in the application.

Following is the content of the modified main activity file src/com.example.tutorialspoint/MainActivity.java.

Following will be the content of res/layout/activity_main.xml file −

Here abc indicates about tutorialspoint logo

Following will be the content of res/values/strings.xml to define two new constants −

Following is the default content of AndroidManifest.xml

Let’s try to run your tutorialspoint application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Android studio, open one of your project’s activity files and click Run icon from the toolbar. Before starting your application, Android studio will display following window to select an option where you want to run your Android application.

Select your mobile device as an option and then check your mobile device which will display following screen −

Now use Compose SMS button to launch Android built-in SMS clients which is shown below −

You can modify either of the given default fields and finally use send SMS button to send your SMS to the mentioned recipient.

Источник

2.2: Part 2 — Sending and Receiving SMS Messages

Contents:

Task 3. Receive SMS messages with a broadcast receiver

To receive SMS messages, use the onReceive() method of the BroadcastReceiver class. The Android framework sends out system broadcasts of events such as receiving an SMS message, containing intents that are meant to be received using a BroadcastReceiver. You need to add the RECEIVE_SMS permission to your app’s AndroidManifest.xml file.

3.1 Add permission and create a broadcast receiver

To add RECEIVE_SMS permission and create a broadcast receiver, follow these steps:

Open the AndroidManifest.xml file and add the android.permission.RECEIVE_SMS permission below the other permission for SMS use:

Receiving an SMS message is permission-protected. Your app can’t receive SMS messages without the RECEIVE_SMS permission line in AndroidManifest.xml.

Name the class «MySmsReceiver» and make sure «Exported» and «Enabled» are checked.

The «Exported» option allows your app to respond to outside broadcasts, while «Enabled» allows it to be instantiated by the system.

  • Open the AndroidManifest.xml file again. Note that Android Studio automatically generates a tag with your chosen options as attributes:
  • 3.2 Register the broadcast receiver

    In order to receive any broadcasts, you must register for specific broadcast intents. In the Intent documentation, under «Standard Broadcast Actions», you can find some of the common broadcast intents sent by the system. In this app, you use the android.provider.Telephony.SMS_RECEIVED intent.

    Add the following inside the tags to register your receiver:

    3.3 Implement the onReceive() method

    Once the BroadcastReceiver intercepts a broadcast for which it is registered ( SMS_RECEIVED ), the intent is delivered to the receiver’s onReceive() method, along with the context in which the receiver is running.

    1. Open MySmsReceiver and add under the class declaration a string constant TAG for log messages and a string constant pdu_type for identifying PDUs in a bundle:
    2. Delete the default implementation inside the supplied onReceive() method.

    In the blank onReceive() method:

    Add the @TargetAPI annotation for the method, because it performs a different action depending on the build version.

    Retrieve a map of extended data from the intent to a bundle .

    Define the msgs array and strMessage string.

    Get the format for the message from the bundle .

    As you enter SmsMessage[] , Android Studio automatically imports android.telephony.SmsMessage .

    Initialize the msgs array, and use its length in the for loop:

    Use createFromPdu(byte[] pdu, String format) to fill the msgs array for Android version 6.0 (Marshmallow) and newer versions. For earlier versions of Android, use the deprecated signature createFromPdu(byte[] pdu).

    Build the strMessage to show in a toast message:

    Get the originating address using the getOriginatingAddress() method.

    Get the message body using the getMessageBody() method.

    Add an ending character for an end-of-line.

  • Log the resulting strMessage and display a toast with it:
  • The complete onReceive() method is shown below:

    3.4 Run the app and send a message

    Run the app on a device. If possible, have someone send you an SMS message from a different device.

    You can also receive an SMS text message when testing on an emulator. Follow these steps:

      Run the app on an emulator.

    Click the (More) icon at the bottom of the emulator’s toolbar on the right side, as shown in the figure below:

  • The extended controls for the emulator appear. Click Phone in the left column to see the extended phone controls:
  • You can now enter a message (or use the default «marshmallows» message) and click Send Message to have the emulator send an SMS message to itself.
  • The emulator responds with a notification about receiving an SMS message. The app should also display a toast message showing the message and its originating address, as shown below:
  • Solution Code

    Android Studio project: SmsMessaging

    Coding challenge

    Challenge: Create a simple app with one button, Choose Picture and Send, that enables the user to select an image from the Gallery and send it as a Multimedia Messaging Service (MMS) message. After tapping the button, a choice of apps may appear, including the Messenger app. The user can select the Messenger app, and select an existing conversation or create a new conversation, and then send the image as a message.

    The following are hints:

    • To access and share an image from the Gallery, you need the following permission in the AndroidManifest.xml file:
    • To enable the above permission, follow the model shown previously in this chapter to check for the READ_EXTERNAL_STORAGE permission, and request permission if necessary.
    • Use the following intent for picking an image:
    • Override the onActivityResult method to retrieve the intent result, and use getData() to get the Uri of the image in the result:
    • Set the image’s Uri, and use an intent with ACTION_SEND , putExtra() , and setType() :
    • Android Studio emulators can’t pass MMS messages to and from each other. You must test this app on real Android devices.
    • For more information about sending multimedia messages, see Sending MMS with Android.

    Android Studio project: MMSChallenge

    Источник

    How to send SMS message in Android

    By mkyong | Last updated: August 29, 2012

    Viewed: 344,702 (+17 pv/w)

    In Android, you can use SmsManager API or device’s Built-in SMS application to send a SMS message. In this tutorial, we show you two basic examples to send SMS message :

    1. SmsManager API
    2. Built-in SMS application

    Of course, both need SEND_SMS permission.

    P.S This project is developed in Eclipse 3.7, and tested with Samsung Galaxy S2 (Android 2.3.3).

    1. SmsManager Example

    Android layout file to textboxes (phone no, sms message) and button to send the SMS message.

    File : SendSMSActivity.java – Activity to send SMS via SmsManager .

    File : AndroidManifest.xml , need SEND_SMS permission.

    2. Built-in SMS application Example

    This example is using the device’s build-in SMS application to send out the SMS message.

    File : res/layout/main.xml – A button only.

    File : SendSMSActivity.java – Activity class to use build-in SMS intent to send out the SMS message.

    Download Source Code

    References

    mkyong

    Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

    Comments

    Hi!
    I’m trying to run the above written program almost the same way you’ve written here but the SMS is not being sent, but I’m getting the (Toast) message that “SMS Sent!”
    I’m using android:versionCode=”1″ and android:versionName=”1.0″,
    Please help.

    Thanks in Advance.
    regards,

    how can i receive acknowledgment when i send SMS with eclipse ADT

    very interesting article! more informative thanks for sharing..
    mobile app development company

    Thanks for posting this information, it is very helpful for all of us. Keep update with your blog.
    Ecommerce Website Designers in Chennai

    How to handle dual sim phones?

    If I am implementing either of the above features as one of the fragments, what changes I am supposed to do?

    hello
    i am writing sample program in Android Studio for Sending SMS.
    that is good for android version 4.5 and lower than.
    and not working in android 5 and upper and fail program.
    please help me.

    whta to do if i have a link of sms service provider for sending bulk message and if want to integrate this with my app to sen d same message to multipale users

    hi,
    i tried your code but am getting “SMS faild,please try again” i.e the catch part
    So what is the solution.Help me out

    As you may have read, his project has been “tested with Samsung Galaxy S2 (Android 2.3.3).”
    Due to change in Google policies, this code should only work on older Android versions.
    You shouldn’t expect this code to work on newer Android (i.e. KitKat, Lollipop and Marshmallow).

    SMS can not be sent by Emulator. We should use real android device for sending sms

    hello all,
    i am developing a new application. it needs mobile number verification. is anyone know how to send message using SMSGateway(API). Plz help me

    Hi i am alireza.i can write code to send smsmessage via java but i want to send smsmessage via java code without saving or show in device inbox can you help me??
    My email: alireza525354@yahoo.com

    try <
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNo, null, sms, null, null);
    Toast.makeText(getApplicationContext(), “SMS Sent!”,
    Toast.LENGTH_LONG).show();
    > catch (Exception e) <
    Toast.makeText(getApplicationContext(),
    “SMS faild, please try again later!”,
    Toast.LENGTH_LONG).show();
    e.printStackTrace();
    >

    Your are nice. when i feel any problem then i look over you site. Sms send is one of the best.
    My question is that I have made this. But problem is that when i send sms to any mobile it cut off some money from my balance.
    Isn’t sms send is free in android?

    My emulator running properly by this code, but the number is not finding any SMS
    . Does this work only on Android phone

    How can I prevent the SmsManager from adding it to the device SMS history? I am trying to make an app that send SMS in the background. But I don’t want the sent SMS to be added to the SMS history. I am using the SmsManager (using the following code) but the sent message gets added to the history.

    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(“phoneNo”, null, “sms message”, null, null);

    How can I prevent it from adding the sent SMS to the device SMS history?

    Источник

    Читайте также:  Получение рут прав андроид через компьютер
    Оцените статью