Sunday, July 26, 2015

android: How to Send large Sms in Android

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"nullnull);
    }    
} 


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... :)

No comments:

Post a Comment