Question

I am developing an App which sends SMS and receives some response.

Now the problem is that the SMS sending works fine in 2.2 froyo but its not working in ICS 4.0.

I have done the following in my code:

  1. added the permission in my Manifest tp receive and send SMS.

    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    
  2. Here is the code i am using in the a Button to send SMS.

    public void sendSms(String phoneNumber,String sms) {      
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber, null ,sms,  null, null);            
    }
    
Was it helpful?

Solution

@rciovati thanks for the suggestion i added two pending intents with SMS method now every thing is working fine Here is the code

SmsManager smsManager=SmsManager.getDefault();
 String SENT = "SMS_SENT";
 String DELIVERED = "SMS_DELIVERED";     
 public void sendSms(String phoneNumber,String sms){
     PendingIntent pi = PendingIntent.getActivity(this, 0,
                new Intent(this, myService.class), 0);   
     PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
                new Intent(SENT), 0);

            PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                new Intent(DELIVERED), 0);

     //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SENT));

        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;                        
                }
            }
        }, new IntentFilter(DELIVERED));


        smsManager.sendTextMessage(phoneNumber, null, sms, sentPI, deliveredPI);

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top