设备关闭时发送短信

时间:2013-03-25 21:54:37

标签: android android-broadcast

我想制作一个广播接收器,在设备关闭时向某个电话号码发送简单的短信。

这是我的代码:

public class ShutDownReceiver extends BroadcastReceiver {

SmsManager sms = SmsManager.getDefault();

@Override
public void onReceive(Context context, Intent intent) {

    Log.i("Log","Device shutting down NOW!");

                        //An actual phone number here
    sms.sendTextMessage("0670#######", null, "This is a text for sms", null, null);

       }

}

到目前为止的结果:

我可以在关闭设备时清楚地看到日志消息。 因此,BroadcastReceiver在关闭时运行良好,但没有短信发送,没有异常。

我尝试使用简单的按钮点击这个短信发送方法,绝对成功。

所以,我知道短信发送方法是正确的,我知道关机接收器是正确的,但由于某种原因它没有发送短信。

有关于此的任何想法? (我使用真实设备,而不是模拟器。)

1 个答案:

答案 0 :(得分:1)

我正在使用....

SmsManager.getDefault().sendTextMessage(number, null, smsText, null, null);

......只要

,它就能很好地发挥作用
  1. 电话号码有效
  2. 消息文本不会太长
  3. 电话服务可用(您是否尝试过其他事件以确保在收到关机之前服务未关闭?)
  4. android.permission.SEND_SMS被授予
  5. 希望这有助于...干杯!

相关问题