【问题标题】:Send SMS leads to Generic Failure发送 SMS 导致一般故障
【发布时间】:2015-02-12 18:54:58
【问题描述】:

我通过我的应用程序使用几乎所有教程中都解释过的非常常见的方式发送 SMS。我将 sendMultipartTextMessage 与“sent Intents”和“delivery Intents”一起使用,然后广播接收器监听结果并打印内容。

但是,每次我尝试发送短信时,即使是大约 10 个字符,我总是会收到“一般故障”。

我的默认 SMS 应用程序运行良好,我可以毫无问题地发送/接收 SMS/MMS,因此这不是网络问题。我不希望我的应用成为我新的默认短信应用,我只是希望它有时能够发送一条短短信。 我尝试了很多东西,但一切都失败了。

这是什么问题,我能做些什么来解决它?

实用程序:

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;

                }

...

【问题讨论】:

  • sendTextMessage() 方法对你有用吗?
  • 很遗憾,没有
  • 你从什么类型的班级发送?也就是说,这是在一个Activity、一个BroadcastReceiver等中吗?
  • 我用静态方法创建了一个 Utils 类:public static void sendSMS(Context context, String destination)。我在为按钮编写的 OnClickListener 中调用该方法。消息存储在共享首选项中
  • 我相信这将是你的问题。该方法参数适用于您的服务中心的号码,它是您网络的一部分,负责处理 SMS 路由和传递的中间人内容。除非您的提供商为您提供了要使用的特定服务中心号码,否则您应该为此传递 null。

标签: android sms


【解决方案1】:

SmsManager#sendMultipartTextMessage() 方法(以及sendTextMessage()sendDataMessage() 方法)中的第二个参数是您的服务中心的号码,而不是发件人的号码。服务中心是您网络中处理 SMS 消息的存储、路由和传递的部分,因此传递无效号码会导致您获得一般故障状态。您只需为此传递null

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多