I have developed an android app that automatically sends an SMS automatically to a device from which it receives an SMS.
My App is working fine on the emulator but when I run it on a real device (android mobile) then it only receives SMSs and does not sends a reponse automatically.
My code is as follows.
public class SMSReciever extends BroadcastReceiver {
String address;
String smsMe = "I Recieved Your SMS";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
address = smsMessage[n].getOriginatingAddress();
}
Toast toast = Toast.makeText(context,"Received SMS: " +
smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(address, null, smsMe, null, null);
}
}
I don't know what is the problem. And why it is not working properly on a real device.
Try this code to send sms.
}
The reason you are not able to send the message is because you have not included:
You can get this message address from your tablet/phone message section.
Make sure that u have make entry in manifest.xml as;
Have you checked if you have SMS permission
in your manifest file ?
also you can try built in SMS application :
Have you added permissions to send SMS'es to your
AndroidManifest.xml
-file?These are related permissions:
As Dya's answer suggests, using PendingIntents, will make it possible to debug the action on your
sendTextMessage()
-method.The error codes are as follows:
According to this tutorial (and Mo Al Sh's answer) there's another built in way of sending SMS'es you could try: