发送短信导致通用失败

时间:2014-12-14 16:07:30

标签: android sms

我正在通过我的应用程序使用一种非常常见的方式发送SMS,这在几乎所有的教程中都有解释。我使用sendMultipartTextMessage和"发送Intents"和#34;传递Intents",然后广播接收器侦听结果并打印东西。

但是,每当我尝试发送短信时,即使是10个字符之类的东西,我总会得到一个"通用失败"。

我的默认短信应用程序运行正常,我可以毫无困难地发送/接收短信/彩信,因此无法解决网络问题。我不希望我的应用程序成为我的新默认短信应用程序,我只是希望它能够发送短信息。 我尝试了很多东西,但一切都失败了。

这是什么问题,我该怎么做才能摆脱它?

Utils:

public static void sendSMS(Context context, String destination) {

    final String srcPhoneNumber = PreferenceManager.getDefaultSharedPreferences(context).getString(OptionsFragment.SMS_SRC_PHONE_NUMBER, null);
    final String message = PreferenceManager.getDefaultSharedPreferences(context).getString(OptionsFragment.SMS_MESSAGE, null);

    if (message == null || message.isEmpty() || destination == null || destination.isEmpty()) {
        Console.log('e', "tag", "sms sending failure");
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("sms");
        broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
        broadcastIntent.putExtra("sms", context.getString(R.string.empty_message));
        MyApplication.getInstance().sendBroadcast(broadcastIntent);
        return ;
    }

    final List<String> phoneNumbers = getPhoneNumbers();
    removeEmptyElement(phoneNumbers);

    SmsManager smsManager = SmsManager.getDefault();
    ArrayList<String> parts = smsManager.divideMessage(message);

    ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
    ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();

    PendingIntent sentPI = PendingIntent.getBroadcast(MyApplication.getInstance(), 0, new Intent("sms"), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(MyApplication.getInstance(), 0, new Intent("sms"), 0);

    for (String part : parts) {
        sentIntents.add(sentPI);
        deliveryIntents.add(deliveredPI);
    }


    SmsManager.getDefault().sendMultipartTextMessage(destination, srcPhoneNumber, parts, sentIntents, deliveryIntents);
}

接收者:

@Override
    public void onReceive(Context context, Intent intent) {

        String s = intent.getAction();
        if (s.equals("sms")) {

            String message = intent.getStringExtra("sms");
            if (message != null) {
                if (message.equals("OK")) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(HomeFragment.this.getActivity());

                    builder.setMessage(context.getString(R.string.sms_send_success))
                            .setCancelable(true)
                            .setTitle("Success");

                    builder.create().show();
                } else {
                    Utils.makeErrorDialog(HomeFragment.this.getActivity(), message);
                }
            }
            else {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:

                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Utils.makeErrorDialog(HomeFragment.this.getActivity(),context.getString(R.string.send_error) + "Generic failure");
                        progressDialog.dismiss();
                        return;

                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Utils.makeErrorDialog(HomeFragment.this.getActivity(),context.getString(R.string.send_error) + "No service");
                        progressDialog.dismiss();
                        return;

                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Utils.makeErrorDialog(HomeFragment.this.getActivity(),context.getString(R.string.send_error) + "Null PDU");
                        progressDialog.dismiss();
                        return;

                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Utils.makeErrorDialog(HomeFragment.this.getActivity(),context.getString(R.string.send_error) + "Radio off");
                        progressDialog.dismiss();
                        return;

                }

...

1 个答案:

答案 0 :(得分:0)

SmsManager#sendMultipartTextMessage()方法中的第二个参数(以及sendTextMessage()sendDataMessage()方法)是针对您的服务中心的号码,而不是发件人的号码。服务中心是网络中处理SMS消息的存储,路由和传递的一部分,因此传递无效的号码将导致您获得通用失败状态。您只需要传递null即可。

相关问题