geeta geeta - 3 months ago 22
Android Question

android app is not sending sms automatically on real device

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.

Answer

Try this code to send sms.

//---sends an SMS---
private void sendSMS(String phoneNumber, String message)
{        
    PendingIntent pi = PendingIntent.getActivity(this, 0,
        new Intent(this, class_name.class), 0);                
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, pi, null);        
}    

}

Comments