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:
Now open you java file and use sendTextMessage() method to send sms. The code of android Java file is given below with explanation:
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:
Now run your project and test application. If you have any doubts please comment. Share and help others. Thanks... :)
<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.activi ty_main);
}
//Called when we click on button
//Called when we click on button
public void msg(View v)
{
//Get default sim services to send sms
//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... :)
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..
ReplyDeleteDon'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.
DeleteBro 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..
Deletecheck in log when you are getting this dialog. you will get answer. if you don't understand than post log message here.
Deleteactually 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...
Deleteyou can use do while ..or while loop for sending message to multiple contacts.
DeleteThanks.Can you give a example for a mms sending......
ReplyDeletei tried this... it shows The type SmsManager is deprecated..... dont knw how to fix it
ReplyDeleteits really helpfull ,how to send sms to 2 numbers
ReplyDeletecan anyone tell how to send sms via internet using android coding....
ReplyDeletethere is task to send sms through android mobile to server without using internet is it possible please help me
ReplyDeleteCan u please give an examle of how to recieeve sms and display it???
ReplyDeleteuse broadcast receiver and register it on sms coming than use message text to display. Just google it and you will get code easily.
DeleteHelp me to make my own application please .. I need it to be my project .
DeleteNice..........
ReplyDeletegetting exception of "onclick " in android studio what should i do???
ReplyDeletebro how to attach both sending msg activity and location finder
ReplyDeleteHi
ReplyDelete