0% found this document useful (0 votes)
38 views6 pages

Practical No - 29

The document describes how to develop an Android program to send and receive SMS messages. It includes XML layout files for the user interface, Java classes for the main activity and broadcast receiver, and the Android manifest file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

Practical No - 29

The document describes how to develop an Android program to send and receive SMS messages. It includes XML layout files for the user interface, Java classes for the main activity and broadcast receiver, and the Android manifest file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

“Develope Program To a) Send SMS b) Receive SMS”

Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70sp"
android:layout_marginTop="100sp"
android:text="Send/Receiver SMS"
android:textColor="#009688"
android:textSize="30sp"
android:textStyle="bold"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter mobile no : "
android:textSize="18sp"
android:layout_marginTop="80sp"
android:layout_marginLeft="30sp"
android:textColor="#673AB7"
android:textStyle="bold"/>
<EditText
android:id="@+id/number"
android:layout_width="150sp"
android:layout_height="wrap_content"
android:layout_marginTop="-40sp"
android:layout_marginLeft="180sp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Message : "
android:textSize="18sp"
android:layout_marginTop="70sp"
android:layout_marginLeft="40sp"
android:textColor="#4CAF50"
android:textStyle="bold"/>

<EditText
android:id="@+id/message"
android:layout_width="200sp"
android:layout_height="wrap_content"
android:layout_marginTop="-40sp"
android:layout_marginLeft="180sp"/>

<Button
android:id="@+id/button"
android:layout_width="200sp"
android:layout_height="wrap_content"
android:text="Send ->"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginTop="70sp"
android:layout_marginLeft="100sp"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

EditText no,msg;
Button sms;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this, new
String[]{Manifest.permission.SEND_SMS},100);
}

no = findViewById(R.id.number);
msg = findViewById(R.id.message);
sms = findViewById(R.id.button);

sms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num = no.getText().toString();
String txt = msg.getText().toString();

try {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(num,null,txt,null,null);
Toast.makeText(MainActivity.this, "Message Sent... :)",
Toast.LENGTH_SHORT).show();
}
catch(Exception e) {
Toast.makeText(MainActivity.this, "Message Not Sent... :(",
Toast.LENGTH_SHORT).show();
}

}
});
}
}

Receive.java

public class Receive extends BroadcastReceiver {


private static final String SMS_RECEIVED_ACTION =
"android.provider.Telephony.SMS_RECEIVED";
String Message;

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onReceive(Context context, Intent intent) {
boolean isVersionM = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M);

if (intent.getAction().equals(SMS_RECEIVED_ACTION)) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
String format = bundle.getString("format");

Object[] objects = (Object[]) bundle.get("pdus");

if (objects != null) {
SmsMessage[] messages = new SmsMessage[objects.length];

for (int i = 0; i < objects.length; i++) {


if (isVersionM) {
messages[i] = SmsMessage.createFromPdu((byte[]) objects[i], format);
} else {
messages[i] = SmsMessage.createFromPdu((byte[]) objects[i]);
}
}
}
}
}
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-feature
android:name="android.hardware.telephony"
android:required="false" />

<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Sms"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

You might also like