简体   繁体   中英

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.

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.

Also I tried to use a wake lock so that the service doesn't get destroyed before SMS is sent. What should i do to make sure that the SMS is sent ? 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. Start a new service inside Broadcastreceiver which will send the SMS at the desired number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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