Even the Log.d is not being called. Can't find the error. I have looked at Android - SMS Broadcast receiver, Android – Listen For Incoming SMS Messages. I have been looking for the solution for last 24 hours or so. Please see and tell where I am wrong.
No exceptions are thrown. SmsListener
is in my package folder, where all other activities are placed.
Update added android:enabled="true"
to receiver. Then also not worked.
I have in my manifest:
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<receiver android:name=".SmsListener" android:exported="false">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
SmsListener.Java Code:
public class SmsListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(MainActivity.TAG,
"in Receiver. intent.getAction():" + intent.getAction());
if (intent.getAction()
.equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras(); // ---get the SMS message passed
// in---
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null) {
// ---retrieve the SMS message received---
try {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
NotifyMe(context, msg_from, msgBody);
}
} catch (Exception e) {
// Log.d("Exception caught",e.getMessage());
}
}
}
}
private void NotifyMe(Context context, String msg_from, String msgBody) {
// do something
}
}
The problem is now solved. Modified receiver in manifest to
It works for me. Hope someone gets benefited by this. Thanks everyone for the help.
Did you tried to register receiver by the code? Below is for example how I did it in one of my projects, - it is working in my case.
SMS Receiver class
Service class - it registers broadcast receiver This is just for example, you can register broadcast receiver in your Activity.
Part of AndroidManifest.xml
Do you have this permission in your manifest android.permission.RECEIVE_SMS?
This is basic, but just make sure