简体   繁体   中英

App does not send and receive SMS

I've written the following code that receives SMS and sends back the same SMS to the sender. The code is running perfectly in Emulator but when I run the code in Mobile it doesn't receive and send an SMS.

public class SmsReceiver extends BroadcastReceiver
{
   public void onReceive(final Context context, Intent intent) 
   {
      //---get the SMS message passed in---
      Bundle bundle = intent.getExtras();        
      SmsMessage[] msgs = null;

      String messageRecieved ="";     
      String result="";
      if (bundle != null)
      {
         //---retrieve the SMS message received---
         Object[] pdus = (Object[]) bundle.get("pdus");
         msgs = new SmsMessage[pdus.length];            
         for (int i=0; i<msgs.length; i++)
         {
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            messageRecieved += msgs[i].getMessageBody().toString();
         }

         //---display the new SMS message---
         Toast.makeText(context, "Sms Recieved: "+messageRecieved,
              Toast.LENGTH_SHORT).show();

         sms.sendTextMessage(sendersPhoneNumber, null, messageRecieved, null, null);
         Toast.makeText(context, "Sms sent back to Sender With Requested Contacts"
            ,Toast.LENGTH_LONG).show();

I have registered the receiver with proper permission in manifest. It works fine in the emulator but not on the phone.

It doesn't show toast.

The issue with your real device might be the low memory problem. Either you have installed some big apps in your phone or the memory is running out of stack. We all know that a SMS is about 20 or 30 KB only, but sometimes we need more than 20 MB of space in our phone, for receiving an SMS.

The solution to this problem is to move your data from phone memory to the SD Card.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM