如何确保启动后发送短信?

时间:2014-04-24 20:06:39

标签: android sms broadcastreceiver intentservice

我正在尝试在设备完成启动后发送短信。在IntentService内部,短信将被发送到某个号码,我使用PendingIntent来确保邮件已发送。

有时短信将被发送,有时它会因为没有服务而失败,所以我试图再次发送短信的方法,直到它被发送但它无法正常工作。

此外,我尝试使用唤醒锁定,以便在发送SMS之前服务不会被销毁。我该怎么做才能确保发送短信?我想尝试发送短信,直到服务准备好并且发送成功。

public class MyService extends IntentService {
PendingIntent sentPI;

public MyService() {
    super("check SIM card");

}

@Override
protected void onHandleIntent(Intent intent) {

    // pending intents to check SMS
    String SENT = "SMS_SENT";

    sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
            SENT), 0);
    // ---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {

                Toast.makeText(getBaseContext(), "SMS sent",
                        Toast.LENGTH_SHORT).show();
                // Releasing wake lock
                WakeLocker.release();
                break;

            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

                Toast.makeText(getBaseContext(), "Generic failure",
                        Toast.LENGTH_SHORT).show();
                sendSMS();
                break;

            case SmsManager.RESULT_ERROR_NO_SERVICE:

                Toast.makeText(getBaseContext(), "No service",
                        Toast.LENGTH_SHORT).show();
                sendSMS();
                break;

            case SmsManager.RESULT_ERROR_NULL_PDU:

                Toast.makeText(getBaseContext(), "Null PDU",
                        Toast.LENGTH_SHORT).show();
                sendSMS();
                break;

            case SmsManager.RESULT_ERROR_RADIO_OFF:

                Toast.makeText(getBaseContext(), "Radio off",
                        Toast.LENGTH_SHORT).show();
                sendSMS();
                break;
            }
        }
    }, new IntentFilter(SENT));


    sendSMS();

}

1 个答案:

答案 0 :(得分:0)

BroadcastReceiver写一个DEVICE_BOOT_COMPLETED,只要你的设备重新启动就会被解雇。在Broadcastreceiver内启动一项新服务,该服务将以所需的号码发送短信。