Reputation: 237
I want my application to receive messages from a particular sender into my application through the BroadcastReceiver, unfortunately its not working and not throwing any error, Below is the code for the BroadcastReceiver:
public void onReceive(Context context, Intent intent) {
// Get Bundle object contained in the SMS intent passed in
Bundle bundle = intent.getExtras();
SmsMessage[]smsm=null;
String sms_str = "";
if (bundle != null) {
// Get the SMS message
Object[] pdus = (Object[]) bundle.get("pdus");
smsm = new SmsMessage[pdus.length];
for (int i = 0; i < smsm.length; i++) {
smsm[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sms_str += "Sent From: " + smsm[i].getOriginatingAddress();
sms_str += "\r\nMessage: ";
sms_str += smsm[i].getMessageBody().toString();
sms_str += "\r\n";
}
Log.d("TAG", sms_str);
// Start Application's MainActivty activity
Intent smsIntent = new Intent(context, MainActivity.class);
smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
smsIntent.putExtra("sms_str", sms_str);
context.startActivity(smsIntent);
}
}
In the Main Activity, I have this code to fetch the intent:
// Get intent object sent from the SMSBroadcastReceiver
Intent sms_intent = getIntent();
Bundle b = sms_intent.getExtras();
if (b != null) {
// Display SMS in the TextView
txtViewSMS.setText(b.getString("sms_str"));
}
My manifest file has:
<!-- Declare SMS Broadcast receiver -->
<receiver android:name=".SMSBReceiver"
android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Kindly assist. Much thanks
Upvotes: 1
Views: 90
Reputation: 25856
Check if you have this permission in AndroidManifest.xml
:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
Also if receiver class name is SMSBroadcastReceiver
then this name must be used in AndroidManifest.xml
:
<receiver android:name=".SMSBroadcastReceiver"
android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Upvotes: 2