title: Android Send SMS from a Service?
body: <p>I am trying to send <strong>SMS</strong> when the battery Level reached a particular value from a <code>Service</code>. I am not getting any errors but the <strong>SMS</strong> is not been sent. </p>
  
  <p>MY CODE:</p>
  
  <pre><code>          import android.app.Service;
            import android.content.BroadcastReceiver;
            import android.content.Context;
            import android.content.Intent;
            import android.content.IntentFilter;
            import android.os.BatteryManager;
            import android.os.IBinder;
            import android.telephony.SmsManager;
            import android.util.Log;
            import android.widget.Toast;
  
  
            public class BatteryStatus extends Service {
        int scale = -1;
            int level = -1;
            int voltage = -1;
            int temp = -1;
            String batteryNumber = "012345567";
            String moderate=" Battery Level is 50 ";
            String low = "Battery Level is low. Need to be charged immediately";
  
  
        public IBinder onBind(Intent intent) {
          return null;
        }
  
        public void onCreate() {
          super.onCreate();
  
          Toast.makeText(getApplicationContext(),"Service Running",  Toast.LENGTH_SHORT).show();
           //BATTERY STATUS 
          BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
  
              @Override
              public void onReceive(Context context, Intent intent) {
                  level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                  scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                  temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
                  voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
                  Log.e("BatteryManager", "level is "+level+"/"+scale+", temp is "+temp+", voltage is  "+voltage);
                  Log.e("NumberBattery ", "Number is "+batteryNumber);
              }
          };
  
          IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
          registerReceiver(batteryReceiver, filter);
          if(batteryNumber!=null &amp;&amp; level==49){
  
                  SmsManager bat = SmsManager.getDefault();
                  bat.sendTextMessage(batteryNumber,null,moderate,null,null);
          }else if(batteryNumber!=null &amp;&amp; level==20){
                  SmsManager bat = SmsManager.getDefault();
                  bat.sendTextMessage(batteryNumber, null,low, null, null);
              }
  
           }
  
  
  
  
             }
  </code></pre>
  
  <p>LOGCAT:</p>
  
  <pre><code>             03-09 03:41:40.390: E/BatteryManager(11086): level is 49/100, temp is 321, voltage is 7574
               03-09 03:41:40.390: E/NumberBattery(11086): Number is 01234567
  </code></pre>
  
  <p>Can you please tell what is wrong her?</p>