发送短信导致一般失败 [英] Send SMS leads to Generic Failure

查看:213
本文介绍了发送短信导致一般失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过我的应用程序使用在pretty解释太多所有教程一种很常见的方式发送短信。我用sendMultipartTextMessage以送意图和交付意图,然后广播接收器监听结果和打印的东西。

不过,每次我尝试发送短信,甚至像10个字符,我总是得到一个通用的失败。

我的默认短信应用工作完美,我可以发送/接收短信/彩信没有任何的烦恼,因此它不能是网络问题。我不希望我的应用程序成为我新的默认短信应用,我只是希望它能够有时发送短短信。
我尝试了很多事情,但一切都已经失败了。

那是什么问题,我可以做些什么来摆脱它?

utils的:

 公共静态无效sendSMS(上下文的背景下,弦乐目的地){    最后弦乐srcPhoneNumber = $p$pferenceManager.getDefaultShared$p$pferences(context).getString(OptionsFragment.SMS_SRC_PHONE_NUMBER,空值);
    最终字符串消息= preferenceManager.getDefaultShared preferences(上下文).getString(OptionsFragment.SMS_MESSAGE,NULL);    如果(消息== NULL || message.isEmpty()||目的地== NULL || destination.isEmpty()){
        CONSOLE.LOG(E,标签,短信发送失败);
        意图broadcastIntent =新的Intent();
        broadcastIntent.setAction(SMS);
        broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
        broadcastIntent.putExtra(SMS,context.getString(R.string.empty_message));
        。MyApplication.getInstance()sendBroadcast(broadcastIntent);
        返回;
    }    最终名单<串GT; PHONENUMBERS = getPhoneNumbers();
    removeEmptyElement(PHONENUMBERS);    SmsManager smsManager = SmsManager.getDefault();
    ArrayList的<串GT;部分= smsManager.divideMessage(消息);    ArrayList的<&的PendingIntent GT; deliveryIntents =新的ArrayList<&的PendingIntent GT;();
    ArrayList的<&的PendingIntent GT; sentIntents =新的ArrayList<&的PendingIntent GT;();    的PendingIntent sentPI = PendingIntent.getBroadcast(MyApplication.getInstance(),0,新的意向(SMS),0);
    的PendingIntent deliveredPI = PendingIntent.getBroadcast(MyApplication.getInstance(),0,新的意向(SMS),0);    对于(字符串部分:部分){
        sentIntents.add(sentPI);
        deliveryIntents.add(deliveredPI);
    }
    。SmsManager.getDefault()sendMultipartTextMessage(目的地,srcPhoneNumber,部件,sentIntents,deliveryIntents);
}

接收器:

  @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){        字符串s = intent.getAction();
        如果(s.equals(SMS)){            字符串消息= intent.getStringExtra(SMS);
            如果(消息!= NULL){
                如果(message.equals(OK)){
                    AlertDialog.Builder建设者=新AlertDialog.Builder(HomeFragment.this.getActivity());                    builder.setMessage(context.getString(R.string.sms_send_success))
                            .setCancelable(真)
                            .setTitle(成功);                    builder.create()显示()。
                }其他{
                    Utils.makeErrorDialog(HomeFragment.this.getActivity(),消息);
                }
            }
            其他{
                开关(的getResult code())
                {
                    案例Activity.RESULT_OK:                        打破;
                    案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Utils.makeErrorDialog(HomeFragment.this.getActivity(),context.getString(R.string.send_error)+一般故障);
                        progressDialog.dismiss();
                        返回;                    案例SmsManager.RESULT_ERROR_NO_SERVICE:
                        Utils.makeErrorDialog(HomeFragment.this.getActivity(),context.getString(R.string.send_error)+无服务);
                        progressDialog.dismiss();
                        返回;                    案例SmsManager.RESULT_ERROR_NULL_PDU:
                        Utils.makeErrorDialog(HomeFragment.this.getActivity(),context.getString(R.string.send_error)+空的PDU);
                        progressDialog.dismiss();
                        返回;                    案例SmsManager.RESULT_ERROR_RADIO_OFF:
                        Utils.makeErrorDialog(HomeFragment.this.getActivity(),context.getString(R.string.send_error)+无线电关);
                        progressDialog.dismiss();
                        返回;                }...


解决方案

SmsManager#sendMultipartTextMessage()方法的第二个参数(还有 sendTextMessage() sendDataMessage()方法)是您的服务中心的数量,而不是发送者的号码。服务中心是处理存储,路由和SMS消息的传输,所以传递一个无效的数字将导致的一般故障的状态你得到你的网络的一部分。您只需通过这一点。

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;

                }

...

解决方案

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.

这篇关于发送短信导致一般失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆