-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmsReceiver.java
50 lines (38 loc) · 1.86 KB
/
SmsReceiver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example.demosmslistener;
import static androidx.constraintlayout.widget.ConstraintLayoutStates.TAG;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SmsReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
Context context;
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus != null) {
for (Object pdu : pdus) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
String sender = smsMessage.getOriginatingAddress();
String messageBody = smsMessage.getMessageBody();
// Here you can do whatever you want with the incoming SMS
// For example, you can display a Toast message
// Toast.makeText(context, "Incoming SMS from: " + sender + "\nMessage: " + messageBody, Toast.LENGTH_LONG).show();
Toast.makeText(context, messageBody, Toast.LENGTH_LONG).show();
Intent intentMain = new Intent(context, MainActivity.class);
intentMain.putExtra("Message", messageBody);
intentMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentMain);
}
}
}
}
}