Send SMS leads to Generic Failure

◇◆丶佛笑我妖孽 提交于 2020-01-14 13:14:50

问题


I am sending SMS through my application using a very common way that is explained in pretty much all tutorials. I use sendMultipartTextMessage with "sent Intents" and "delivery Intents", then a Broadcast receiver listen for the results and print things.

But, everytime I try to send a SMS, even with something like 10 characters, I always get a "Generic failure".

My default SMS app is working perfectly and I can send/receive SMS/MMS without any troubles so it can't be a network issue. I don't want my app to become my new default SMS app, I just want it to be able to send a short SMS sometimes. I tried a lot of things but everything has failed.

What is that issue and what can I do to get rid of it ?

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);
}

Receiver :

@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:


The second parameter in the SmsManager#sendMultipartTextMessage() method (as well as the sendTextMessage() and sendDataMessage() methods) is for the number of your service center, not the sender's number. A service center is the part of your network that handles the storage, routing, and delivery of SMS messages, so passing an invalid number would result in the generic failure status you're getting. You simply need to pass null for this.



来源:https://stackoverflow.com/questions/27471155/send-sms-leads-to-generic-failure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!