I am creating an application that will grab the contents of a text message and display it in a pop up. My Broadcast receiver is not shooting when a text message is received.
Broadcast Receiver class
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] msgs = new SmsMessage[pdus.length];
/** sms sender phone */
String smsSender = "";
/** body of received sms */
String smsBody = "";
/** timerstamp */
long timestamp = 0L;
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
smsSender += msgs[i].getOriginatingAddress();
smsBody += msgs[i].getMessageBody().toString();
timestamp += msgs[i].getTimestampMillis();
}
intent.putExtra("sender", smsSender);
intent.putExtra("body", smsBody);
intent.putExtra("timestamp", timestamp);
}
}
Dialog Activity public class PopSMSActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showDialog();
}
private void showDialog() {
final String sms_sender = getIntent().getStringExtra("sender");
final String sms_body = getIntent().getStringExtra("body");
final long timestamp = getIntent().getLongExtra("timestamp", 0L);
final String display = sms_sender + "\n" + sms_body + "\n" + timestamp;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(display)
.setCancelable(false)
.setPositiveButton("Reply",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// reply by calling SMS program
smsReply(sms_sender, sms_body);
}
})
.setNegativeButton("Close",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// go back to the phone home screen
goHome();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private void smsReply(String sender, String body) {
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("address", sender);
sendIntent.putExtra("sms_body", body);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
this.finish(); // close this Activity now
}
private void goHome() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
this.finish();
}
}
Manifest
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="com.example.intereceptsms.SMSReceiver" >
<intent-filter
android:exported="true"
android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name="com.example.intereceptsms.PopSMSActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
</manifest>
I am not pulling up any errors, so there must be something that I am missing, any help will be great. Thank You.
My Broadcast receiver is not shooting when a text message is received
That is because it is not doing anything, other than creating an Intent
, which it then ignores. Presumably, your plan is to start PopSMSActivity
, in which case you need the startActivity()
call.
Also note that, while this is fine for experimentation, users may get very irritated if something takes over the foreground just because a text message arrived.