Wednesday, 30 December 2015

How to send SMS in android without user interaction


       Here I am going to explain how to send text message(SMS) in android without user interaction.


Step 1).Declare BroadCastReceiver and PendingIntent in your actvity;


    BroadcastReceiver sendBroadcastReceiver;
    BroadcastReceiver deliveryBroadcastReceiver;
    PendingIntent deliveredPI;
    PendingIntent sentPI;


Step 2).Call this function


   private void sendSMS(String phoneNumber, String message) {
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        sentPI= PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
               

       deliveredPI= PendingIntent.getBroadcast(this, 0,                                                                                                        newIntent(DELIVERED),0);                   
        //---SMS has been sent---
        sendBroadcastReceiver= 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;
                }
            }
        };
        registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));
        //---when the SMS has been delivered---
        deliveryBroadcastReceiver= 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;
                }
            }
        };


       registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
       SmsManager sms = SmsManager.getDefault();
       sms.sendTextMessage(phoneNumber, null,message sentPI, deliveredPI);

    }


Step 3) UnRegister the receiver 

   @Override
    protected void onStop() {
        try {
            if (sentPI != null) {
                unregisterReceiver(sendBroadcastReceiver);
                sentPI = null;
            }

            if (deliveredPI != null) {
                unregisterReceiver(deliveryBroadcastReceiver);
                deliveredPI =null;
            }
        } catch (Exception e) {

        }
        super.onStop();
    }

Step 4)Add SMS permission in your Manifest file

 <uses-permission android:name="android.permission.SEND_SMS" />


And Make Sure you have SMS plan


Happy coding:)