-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editTextPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter phone number"
android:inputType="phone"/>
<EditText
android:id="@+id/editTextMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextPhoneNumber"
android:layout_marginTop="16dp"
android:hint="Enter message"/>
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextMessage"
android:layout_marginTop="16dp"
android:text="Send"/>
</RelativeLayout>
2. Update the Java code (MainActivity.java):
-
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextPhoneNumber, editTextMessage;
private Button btnSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextPhoneNumber = findViewById(R.id.editTextPhoneNumber);
editTextMessage = findViewById(R.id.editTextMessage);
btnSend = findViewById(R.id.btnSend);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage();
}
});
}
private void sendMessage() {
String phoneNumber=editTextPhoneNumber.getText().toString();
String message = editTextMessage.getText().toString();
// Check if the phone number and message are not empty
if (!phoneNumber.isEmpty() && !message.isEmpty()) {
SmsManager smsManager = SmsManager.getDefault();
// Create a PendingIntent for the delivery report
PendingIntent deliveryIntent=PendingIntent.getBroadcast(this,0,new Intent("SMS_DELIVERED"),0);
// Send the SMS with delivery report
smsManager.sendTextMessage(phoneNumber,null,message,null,deliveryIntent);
Toast.makeText(this,"Mesg. sent",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,"Please enter phone number and message", Toast.LENGTH_SHORT).show();
}
}
}
3.Handle the Delivery Report:(SmsReceiver.java):
-
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case MainActivity.RESULT_OK:
Toast.makeText(context, "Message delivered", Toast.LENGTH_SHORT).show();
break;
case MainActivity.RESULT_CANCELED:
Toast.makeText(context, "Message delivery failed", Toast.LENGTH_SHORT).show();
break;
}
}
}
4.Register BroadcastReceiver in the Manifest:(AndroidManifest.xml):
-
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="SMS_DELIVERED" />
</intent-filter>
</receiver>
No comments:
Post a Comment