I want my broadcast receiver class to receive as well as read the incoming message and call a Service class when the incoming message and the message sent is the same. This is my receiver class:
import static com.example.savior.contacts.stringBuilder;
public class MyReceiver extends BroadcastReceiver {
SmsMessage[] msgs;
Bundle bundle;
AudioManager audioManager;
String message="";
SmsMessage currentMessage;
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//Log.v("message",stringBuilder);
bundle = intent.getExtras(); //---get the SMS message passed in---
msgs = null;
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
if (bundle != null) {
//---retrieve the SMS message received---
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (Object o : pdusObj) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
currentMessage = SmsMessage.createFromPdu((byte[]) o, bundle.getString("format"));
} else {
currentMessage = SmsMessage.createFromPdu((byte[]) o);
}
String senderNum = currentMessage.getDisplayOriginatingAddress();
message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);
// Show alert
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, duration);
toast.show();
}
if (message.equals(stringBuilder)) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int nba = audioManager.getRingerMode();
if (nba == AudioManager.RINGER_MODE_SILENT) {
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
Intent j = new Intent(context, Ringer_modifier.class);
context.startService(j);
}
//Log.d("message", stringBuilder);
} else
Toast.makeText(context, "bundle is null", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(context, "Receive permission not allowed in this app", Toast.LENGTH_SHORT).show();
}
}
But, the receiver class doesn't read the incoming sms received and no toast is shown whatsoever. Also, the Service class isn't called, even though the strings message and string are equal.
My permissions included inside manifest:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="6000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
In brief, my broadcast receiver is not able to read the incoming messages. Why?
from Broadcast receiver not working for text messages in android Oreo and above devices
No comments:
Post a Comment