First we will create a project Android And as soon as we create the project we will see two files MainActivity and activity_main .We will first create the design in activity_main, what type we need
activity_main
<?xml version="1.0" encoding="utf-8"?>
<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">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your message here"
android:layout_margin="20dp"/>
<EditText
android:id="@+id/edittext2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter mobile number"
android:layout_margin="20dp"/>
<Button
android:id="@+id/sendMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Message"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
In this layout we have used two edittext one button Through edittext we will input the message and mobile number from the user and send the message through the button.MainActivity.java
package com.example.sendmessageexample;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_CODE = 10;
private Button sendBtn;
private EditText edit_message, edit_number;
String msg, phoneNo;
@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) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.SEND_SMS)) {
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST_CODE);
}
}
sendBtn = findViewById(R.id.sendMessage);
edit_message = findViewById(R.id.editText);
edit_number = findViewById(R.id.edittext2);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMessage();
}
});
}
private void sendMessage() {
msg=edit_message.getText().toString();
phoneNo=edit_number.getText().toString();
SmsManager smsManager= SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo,null,msg,null,null);
Toast.makeText(this, "Message Sent", Toast.LENGTH_SHORT).show();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode)
{
case PERMISSION_REQUEST_CODE:
{
if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Permission not granted..", Toast.LENGTH_SHORT).show();
}
}
}
}
}
Here we have used onclick on the button, as soon as the user clicks on the button, the message will be sent.But before that we have to take Runtime permission of send message.Using SmsManager we will write program using message sendTextMessage.And in the last we will not forget to give permission in the manifest.like thisManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sendmessageexample">
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SendMessageExample">
<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>
Thank you
0 Comments
Please do not enter any spam link in the comment box
Emoji