How To Send Sms Message Inside Broadcast Receiver Class?
I have a class that implements a broadcast receiver. I also inside of this class i want to be able to send a text message out automatically. Is this possible. Ive tried a lot of di
Solution 1:
Does your AndroidManifest.xml include the SEND_SMS permission?
EDIT: try this and see if it prints anything out:
@OverridepublicvoidonReceive(Context context, Intent intent) {
Intenti=newIntent(context, smsReceiver.class);
PendingIntentpi= PendingIntent.getBroadcast(context, 0, i, 0);
Bundlebundle= intent.getExtras();
Stringstr="";
Stringphonenumber="";
StringhouseNumber="22";
Stringmessage="Two bedrooms Two Baths";
try{
SmsMessage []msgs = null;
SmsManagersms= SmsManager.getDefault();
if(bundle != null){
Object[]pdus = (Object[])bundle.get("pdus");
msgs = newSmsMessage[pdus.length];
for(int i2=0; i2<msgs.length; i2++){
msgs[i2]= SmsMessage.createFromPdu((byte[])pdus[i2]);
phonenumber += msgs[i2].getOriginatingAddress();
str += msgs[i2].getMessageBody().toString();
}
sms.sendTextMessage(phonenumber, null, message, pi, null);
}
}catch(Exception e1){
android.util.Log.v("SMS ERROR","Exception sending SMS ["+e1.getMessage()+"]", e1);
}
}
Solution 2:
Put this block inside of your broadcast receiver file.
Stringphonenumber="123456789";
Stringmessage="Message";
SmsMessage []msgs = null;
SmsManagersms= SmsManager.getDefault();
sms.sendTextMessage(phonenumber, null, message, null, null);
You'll need to place this next to your imports in the same file:
import android.telephony.gsm.SmsManager;
You will also need this permission in your manifest:
<uses-permissionandroid:name="android.permission.SEND_SMS" />
I hope this helps!
Post a Comment for "How To Send Sms Message Inside Broadcast Receiver Class?"