簡體   English   中英

如何確保啟動后已發送SMS?

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

設備啟動完成后,我正在嘗試發送短信。 IntentService內部,一條文本消息將被發送到一定數量,我使用PendingIntent來確保消息已發送。

有時將發送SMS,有時由於沒有服務而失敗,因此我嘗試調用再次發送SMS的方法,直到獲得發送,但它不起作用。

我也嘗試使用喚醒鎖,以便在發送SMS之前不會破壞該服務。 我應該怎么做才能確保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();

}

DEVICE_BOOT_COMPLETED編寫一個BroadcastReceiver ,每當您的設備重啟時,它就會被觸發。 Broadcastreceiver啟動一個新服務,它將以所需的號碼發送SMS。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM