简体   繁体   English

如何确保启动后已发送SMS?

[英]How to insure that SMS has been sent after boot up?

i am trying to send an SMS after the device is done booting. 设备启动完成后,我正在尝试发送短信。 Inside an IntentService a text message will be sent to a certain number and i used PendingIntent to make sure that the message is sent. IntentService内部,一条文本消息将被发送到一定数量,我使用PendingIntent来确保消息已发送。

Sometimes the SMS will be sent and sometimes it will fail because there is no service so i tried to call the method that send the SMS again until it gets sent but it's not working. 有时将发送SMS,有时由于没有服务而失败,因此我尝试调用再次发送SMS的方法,直到获得发送,但它不起作用。

Also I tried to use a wake lock so that the service doesn't get destroyed before SMS is sent. 我也尝试使用唤醒锁,以便在发送SMS之前不会破坏该服务。 What should i do to make sure that the SMS is sent ? 我应该怎么做才能确保SMS已发送? i want to try sending SMS until service is ready and sending is success. 我想尝试发送短信,直到服务准备就绪并且发送成功为止。

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();

}

Write a BroadcastReceiver for DEVICE_BOOT_COMPLETED which will get fired whenever your device is rebooted. DEVICE_BOOT_COMPLETED编写一个BroadcastReceiver ,每当您的设备重启时,它就会被触发。 Start a new service inside Broadcastreceiver which will send the SMS at the desired number. Broadcastreceiver启动一个新服务,它将以所需的号码发送SMS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM