SMS-SEND and RECEIVE
Permissions-------
<uses-permission android:name="[Link].RECEIVE_SMS"/>
<uses-permission android:name="[Link].READ_SMS"/>
<uses-permission android:name="[Link].SEND_SMS"/>
[Link]-----------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/phonenumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Phone Number"
android:inputType="phone" />
<EditText
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Message"
android:inputType="textMultiLine" />
<Button
android:id="@+id/sendSMS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS"
android:layout_gravity="center" />
</LinearLayout>
[Link]------------------
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText phoneNumber, message;
Button sendSMS;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
phoneNumber = findViewById([Link]);
message = findViewById([Link]);
sendSMS = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
sendSMS();
}
});
}
private void sendSMS() {
String phone = [Link]().toString().trim();
String msg = [Link]().toString().trim();
if (![Link]() && ![Link]()) {
SmsManager smsManager = [Link]();
[Link](phone, null, msg, null, null);
[Link](this, "SMS Sent!", Toast.LENGTH_SHORT).show();
} else {
[Link](this, "Enter phone number and message",
Toast.LENGTH_SHORT).show();
}
}
}
[Link]----------------------------------------
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle= [Link]();
if (bundle != null) {
// Retrieve the SMS Messages received
Object[] sms = (Object[]) [Link]("pdus");
String format = [Link]("format");
// For every SMS message received
for (int i=0; i < [Link]; i++)
{
// Convert Object array
SmsMessage smsMessage = [Link]((byte[])
sms[i], format);
String sender = [Link]();
String message = [Link]();
[Link](context, "SMS from: " + sender + "\nMessage: " +
message,
Toast.LENGTH_LONG).show();
}
}
}
}
<receiver android:name=".SmsReceiver"
android:permission="[Link].BROADCAST_SMS"
android:exported="true">
<intent-filter>
<action
android:name="[Link].SMS_RECEIVED"></action>
</intent-filter>
</receiver>