Search Tutorials

Loading...

Sunday, 29 September 2013

How to Send large Sms in Android

In this application, we will learn how to send Sms without getting any sending report. So create your new project and drop one button to XML file which will use to send Sms. Here we gave a simple example, you all can take mobile number and text message from edit text and send to particular user. The code of android XML file is given below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="17dp"
        android:text="Send Message"
        android:onClick="msg" />
</RelativeLayout>

Now open you java file and use sendTextMessage() method to send sms. The code of android Java file is given below with explanation:

package selecom.alert; //your project name
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.app.Activity;

public class MainActivity extends Activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);   
    }
//Called when we click on button
    public void msg(View v)
    {
//Get default sim services to send sms
      SmsManager sms=SmsManager.getDefault();
//Enter mobile number and text message
      sms.sendTextMessage("8527801400" ,null, "hello Mohsin i am using your App", null, null);
    }    
}


If your message is larger than 160 chars than the above method will not work. To send large message, we will have to divide large message into small messages and send to a particular person. 

             String str="your large message";
             String number="person mobile number";
             SmsManager smsManager = SmsManager.getDefault();
            ArrayList<String> parts = smsManager.divideMessage(str);
            smsManager.sendMultipartTextMessage(number, null, parts, null, null);

If you want to send a particular message to multiple people than simple use string array and for loop like this:
           
            String[] numbers={ "8527801400","8307489274"} //add more
            for(int i=0; i<numbers.length; i++)
            {
               smsManager.sendMultipartTextMessage(numbers[i], null, parts, null, null);
            }

Because we are using service of android phone in our application so we have to take permission to use it and i already discuss about Implicit Intent. The code of AndroidManifist.xml file is given below:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="selecom.alert"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" />
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="selecom.alert.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Now run your project and test application. If you have any doubts please comment. Share and help others. Thanks... :)

19 comments:

  1. i want to do the same thing as above...but i want to access the mobile number from the database...i tried it it is showing error..can you help me..

    ReplyDelete
    Replies
    1. Don't post blank text bro...i removed both comments and it easy to send SMS on mobile. Just get number from database and use in SendTextMassge() method.

      Delete
    2. Bro i dint post anything empty act i pasted my code what i tried..but it dint display...thank you bro...there is no error in my code..but when i run it .it shows "unfortunately app_name has stoped"...sowat to do..

      Delete
    3. check in log when you are getting this dialog. you will get answer. if you don't understand than post log message here.

      Delete
    4. actually it work with only first record of table.. but how to make it with second record...we cannot use (do..while) in this case and goto is not possible in android...actually i am new for android coding...so can yu help me...

      Delete
    5. you can use do while ..or while loop for sending message to multiple contacts.

      Delete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. Thanks.Can you give a example for a mms sending......

    ReplyDelete
  5. i tried this... it shows The type SmsManager is deprecated..... dont knw how to fix it

    ReplyDelete
  6. its really helpfull ,how to send sms to 2 numbers

    ReplyDelete
  7. can anyone tell how to send sms via internet using android coding....

    ReplyDelete
  8. there is task to send sms through android mobile to server without using internet is it possible please help me

    ReplyDelete
  9. Can u please give an examle of how to recieeve sms and display it???

    ReplyDelete
    Replies
    1. use broadcast receiver and register it on sms coming than use message text to display. Just google it and you will get code easily.

      Delete
    2. Help me to make my own application please .. I need it to be my project .

      Delete
  10. This comment has been removed by the author.

    ReplyDelete
  11. getting exception of "onclick " in android studio what should i do???

    ReplyDelete

Back to Top