Android从服务发送短信?

时间:2012-03-09 13:05:58

标签: android service sms

当电池电量达到Service的特定值时,我正尝试发送短信。我没有收到任何错误,但短信尚未发送。

我的代码:

          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 && level==49){

                SmsManager bat = SmsManager.getDefault();
                bat.sendTextMessage(batteryNumber,null,moderate,null,null);
        }else if(batteryNumber!=null && level==20){
                SmsManager bat = SmsManager.getDefault();
                bat.sendTextMessage(batteryNumber, null,low, null, null);
            }

         }




           }

logcat的:

             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

你能说出她的错误吗?

1 个答案:

答案 0 :(得分:1)

这是因为你从不发送短信。

执行以下行时:

if (batteryNumber != null && level == 49){

level尚未初始化。

更改您的代码:

@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);
    if (batteryNumber != null && level == 49) {
        SmsManager bat = SmsManager.getDefault();
        bat.sendTextMessage(batteryNumber,null,moderate,null,null);
    } else if(batteryNumber!=null && level==20){
        SmsManager bat = SmsManager.getDefault();
        bat.sendTextMessage(batteryNumber, null,low, null, null);
    }
}