SMS - 应用程序未运行时处理交付报告

时间:2015-09-19 07:13:20

标签: android smsmanager

我有一个应用程序,其中SMS被发送到特定号码并且将跟踪递送报告。我正在使用this SO链接中的代码,当应用程序运行时,交付报告就可以了。

我如何处理这种情况,例如,我发送SMS的号码被关闭(或)不在网络区域(OR),短信传递由于某种原因被延迟。现在当SMS发送时,我将收到发送报告,但是,我不知道如何处理这些情况。

有人可以指点我这个方向。

注意:此代码位于片段

我的代码到现在为止:

sendSMS方法:

   private void sendSMS(JSONObject object, String sms_message) {

        try {

            PendingIntent sentPI = PendingIntent.getBroadcast(activity, 0, new Intent(
                    SENT), 0);

            PendingIntent deliveredPI = PendingIntent.getBroadcast(activity, 0,
                    new Intent(DELIVERED), 0);

            activity.registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));
            activity.registerReceiver(deliveryBroadcastReciever, new IntentFilter(DELIVERED));

            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(object.getString("official_mobile"), null, sms_message, sentPI, deliveredPI);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

广播接收器:

class sentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent arg1) {
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Log.d(Const.DEBUG, "Message Sent Successfully");
                Toast.makeText(activity, "SMS Sent Successfully", Toast.LENGTH_SHORT)
                        .show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Log.d(Const.DEBUG, "Message Sending Failed");
                Toast.makeText(activity, "Generic failure",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Log.d(Const.DEBUG, "Message Sending Failed");
                Toast.makeText(activity, "No service",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Log.d(Const.DEBUG, "Message Sending Failed");
                Toast.makeText(activity, "Null PDU", Toast.LENGTH_SHORT)
                        .show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Log.d(Const.DEBUG, "Message Sending Failed");
                Toast.makeText(activity, "Radio off",
                        Toast.LENGTH_SHORT).show();
                break;
        }

    }
}


class deliverReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent arg1) {
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Log.d(Const.DEBUG, "Message Delivered Successfully");
                Toast.makeText(activity, "SMS Delivered",
                        Toast.LENGTH_SHORT).show();
                break;
            case Activity.RESULT_CANCELED:
                Log.d(Const.DEBUG, "Message Delivery Failed");
                Toast.makeText(activity, "SMS Delivery Failed.",
                        Toast.LENGTH_SHORT).show();
                break;
        }

    }
}

0 个答案:

没有答案