The Android framework provides full access to SMS functionality using the SmsManager class.
Make an Android project and have a main activity with name SendSMSActivity.java with the following code
package com.example.sendsms;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class SendSMSActivity extends Activity {
SendSMS mSender = new SendSMS();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sendit(View v) {
boolean success = mSender
.sendSMSMessage("+917829955125",
"Hi, sms application is working fine , love u :)");
Toast.makeText(
this,
"Message sent " + (success ? "successfully" : "unsuccessfully"),
Toast.LENGTH_SHORT).show();
}
}
the SendSMS.java has following code
package com.example.sendsms;
import java.util.ArrayList;
import android.telephony.SmsManager;
import android.util.Log;
/** The code for dealing with the SMS manager;
* called from the GUI code.
*/
public class SendSMS {
static String TAG = "SendSMS";
SmsManager mSMSManager = null;
/* The list of message parts our messge
* gets broken up into by SmsManger */
ArrayList<String> mFragmentList = null;
/* Service Center - not used */
String mServiceCentreAddr = null;
SendSMS() {
mSMSManager = SmsManager.getDefault();
}
/* Called from the GUI to send one message to one destination */
public boolean sendSMSMessage(
String aDestinationAddress,
String aMessageText) {
if (mSMSManager == null) {
return (false);
}
mFragmentList = mSMSManager.divideMessage(aMessageText);
int fragmentCount = mFragmentList.size();
if (fragmentCount > 1) {
Log.d(TAG, "Sending " + fragmentCount + " parts");
mSMSManager.sendMultipartTextMessage(aDestinationAddress,
mServiceCentreAddr,
mFragmentList, null, null);
} else {
Log.d(TAG, "Sendine one part");
mSMSManager.sendTextMessage(aDestinationAddress,
mServiceCentreAddr,
aMessageText, null, null);
}
return true;
}
}
the main.xml is having the following layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/send"
android:onClick="sendit"/>
</LinearLayout>
Set the permission in the AndroidManifest XML file to send SMS:
<uses-permission android:name="android.permission.SEND_SMS">
Great post.Thanks for one marvelous posting!The information was very useful, all these software are really helpful. visit this article for Best Software Development tools in 2020
ReplyDelete