以编程方式发送短信在某些国家/地区不起作用(马来西亚)

时间:2018-01-24 08:14:16

标签: android sms send

我正在开发一个以编程方式将短信发送到手机号码的Android应用程序。它对本地号码工作非常好,并在国内发送短信。但是当我在像马拉西亚这样的另一个国家使用它时,短信不起作用。 如果您有关于该问题的任何信息,请提供帮助。

下面是我试过的代码。

private void sendSMSwithDeliveryReport(String phoneNumber, String message) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

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

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

    sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

    for (int i = 0; i < contacts.size(); i++) {
        // manager.sendTextMessage(contacts.get(i).getNumber(), "", text, null, null);
        sms.sendTextMessage(contacts.get(i).getNumber(), null, message, sentPI, deliveredPI);
        Log.e("sos message", contacts.get(i).getNumber());
    }

}

SmsManager sms;
private BroadcastReceiver smsSentbroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(context, "SMS sent",
                            Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(context, "Generic failure",
                            Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(context, "No service",
                            Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(context, "Null PDU",
                            Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(context, "Radio off",
                            Toast.LENGTH_SHORT).show();
                break;
        }
    }
};

private BroadcastReceiver smsdeliveryReport = new BroadcastReceiver(){
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "SMS delivered",
                            Toast.LENGTH_SHORT).show();
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(getBaseContext(), "SMS not delivered",
                            Toast.LENGTH_SHORT).show();
                break;
        }
    }
};

@Override
protected void onResume() {
    super.onResume();
    try {
        //---when the SMS has been sent---
        registerReceiver(smsSentbroadcastReceiver, new IntentFilter("SMS SENT"));

        //---when the SMS has been delivered---
        registerReceiver(smsdeliveryReport, new IntentFilter("SMS DELIVERED"));
    }
    catch (Exception ignored) {

    }
}

@Override
protected void onPause() {
    super.onPause();
    try {
        unregisterReceiver(smsdeliveryReport);
        unregisterReceiver(smsSentbroadcastReceiver);
    }
    catch (Exception ignored) {

    }
}

0 个答案:

没有答案