无法发送用MMS SmsManager [英] Unable to send MMS using SmsManager

查看:1030
本文介绍了无法发送用MMS SmsManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个应用程序,将发送一个彩信的没有的使用原生的Andr​​oid消息应用程序。我跟着例子<一个href=\"https://github.com/android/platform_development/blob/master/samples/ApiDemos/src/com/example/android/apis/os/MmsMessagingDemo.java\"相对=nofollow>此处。我的日志语句似乎正确打印,但我不明白,为什么没有被发送彩信。

I am trying to make an app that would send a MMS without using the native Android messaging app. I followed the example here. My log statements seem to be correctly printing, but I can't figure out why the MMS is not being sent.

另外一个不同的音符,我有点困惑,其中的例子中的附件(如图像)被选中,以通过彩信发送。我试图演示导入到Android的工作室,但我遇到了问题。

Also on a different note, I am a bit confused about where in the example the attachment (like an image) is being selected to send as MMS. I tried to import the demo into Android Studio but I ran into issues.

我对功能的发送彩信是如下:

 public void sendMMS() {
    Log.d(TAG, "sendMMS()");
    Random random = new Random();
    final String fileName = "send." + String.valueOf(Math.abs(random.nextLong())) + ".dat";

    final File mSendFile = new File(mContext.getCacheDir(), fileName);
    // Making RPC call in non-UI thread
    AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
        @Override
        public void run() {
            final byte[] pdu = buildPdu();
            Uri writerUri = (new Uri.Builder())
                    .authority("com.example.appname")
                    .path(fileName)
                    .scheme(ContentResolver.SCHEME_CONTENT)
                    .build();
            Log.d(TAG, "sendMMS(): Uri: " + writerUri.toString());
            FileOutputStream writer = null;
            Uri contentUri = null;
            try {
                writer = new FileOutputStream(mSendFile);
                writer.write(pdu);
                contentUri = writerUri;
                Log.d(TAG, "sendMMS(): just wrote file");
            } catch (final IOException e) {
                Log.d(TAG, "sendMMS(): FAILED: couldn't write file");
            } finally {
                if (writer != null) {
                    try {
                        writer.close();
                    } catch (IOException e) {
                    }
                }
            }
            if (contentUri != null) {
                SmsManager.getDefault().sendMultimediaMessage(mContext, contentUri, null, null, null);
                Log.d(TAG, "sendMMS(): just sent");
            } else {
                Log.d(TAG, "sendMMS(): FAILED: couldn't write file so didn't send");
            }
        }
    });

}

辅助功能

    private byte[] buildPdu() {
    final SendReq req = new SendReq();
    // from
    final String lineNumber = getSimNumber();
    if (!TextUtils.isEmpty(lineNumber)) {
        req.setFrom(new EncodedStringValue(lineNumber));
    }
    // to
    String[] destsArray = mDestList.toArray(new String[mDestList.size()]);
    EncodedStringValue[] encodedNumbers = EncodedStringValue.encodeStrings(destsArray);
    if (encodedNumbers != null) {
        req.setTo(encodedNumbers);
    }
    // date
    req.setDate(System.currentTimeMillis() / 1000);
    // body
    PduBody body = new PduBody();
    // message text
    final int size = addMessagePart(body, true/* add text smil */);
    req.setBody(body);
    // message size
    req.setMessageSize(size);
    // message class
    req.setMessageClass(PduHeaders.MESSAGE_CLASS_PERSONAL_STR.getBytes());
    // expiry
    req.setExpiry(DEFAULT_EXPIRY_TIME);
    try {
        // priority
        req.setPriority(DEFAULT_PRIORITY);
        // delivery report
        req.setDeliveryReport(PduHeaders.VALUE_NO);
        // read report
        req.setReadReport(PduHeaders.VALUE_NO);
    } catch (InvalidHeaderValueException e) {}
    return new PduComposer(mContext, req).make();
}

private String getSimNumber() {
    TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
    return telephonyManager.getLine1Number();
}

private int addMessagePart(PduBody pb, boolean addTextSmil) {
    PduPart part = new PduPart();
    part.setCharset(CharacterSets.UTF_8);
    part.setContentType(ContentType.TEXT_PLAIN.getBytes());
    part.setContentLocation(TEXT_PART_FILENAME.getBytes());
    int index = TEXT_PART_FILENAME.lastIndexOf(".");
    String contentId = (index == -1) ? TEXT_PART_FILENAME : TEXT_PART_FILENAME.substring(0, index);
    part.setContentId(contentId.getBytes());
    part.setData(mMessage.getBytes());
    pb.addPart(part);
    if (addTextSmil) {
        String smil = String.format(sSmilText, TEXT_PART_FILENAME);
        addSmilPart(pb, smil);
    }
    return part.getData().length;

}

private void addSmilPart(PduBody pb, String smil) {
    PduPart smilPart = new PduPart();
    smilPart.setContentId("smil".getBytes());
    smilPart.setContentType(ContentType.APP_SMIL.getBytes());
    smilPart.setContentLocation("smil.xml".getBytes());
    smilPart.setData(smil.getBytes());
    pb.addPart(0, smilPart);
}

相关部分我清单

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

相关实例变量

    private final long DEFAULT_EXPIRY_TIME = 7 * 24 * 60 * 60;
private final String TEXT_PART_FILENAME = "text_0.txt";
private final int DEFAULT_PRIORITY = PduHeaders.PRIORITY_NORMAL;
private String mMessage;
private ArrayList<String> mDestList;
private Context mContext;
private static final String sSmilText =
        "<smil>" +
                "<head>" +
                    "<layout>" +
                        "<root-layout/>" +
                        "<region height=\"100%%\" id=\"Text\" left=\"0%%\" top=\"0%%\" width=\"100%%\"/>" +
                    "</layout>" +
                "</head>" +
                "<body>" +
                    "<par dur=\"8000ms\">" +
                        "<text src=\"%s\" region=\"Text\"/>" +
                    "</par>" +
                "</body>" +
        "</smil>";

我已经做输入检查,所以按时间sendMMS()被调用时,我的消息,destList不为null。

I already do input checks, so by the time sendMMS() is called, my message and destList are not null.

推荐答案

流程应该是这样:


  1. 创建彩信发送请求。 - 新SendReq()和配置它的日期,身体,等

  2. 创建彩信正文 - 新PduBody()

  3. 通过新PduPart()每个附件创建零件,并添加身体: body.addPart(PDU)

  4. 体加入请求 - req.setBody(体)

  5. 转换发送请求为[]随时通过调用发送一个字节新PduComposer(背景下,mySendReq)。使() - 请注意,你需要从Android的源$ C ​​$ C复制大量的code来获得PduComposer类。

  6. 现在,是有趣的部分 - 你的byte []保存到只有您的应用程序可以访问本地文件,并添加的ContentProvider 类,允许其他应用程序请求访问您的文件,这是 MmsFileProvider 类的示例应用程序,不要忘记申报您的供应商在你的清单文件。

  7. 现在,当你调用 SmsManager.sendMultimediaMessage API,您的文件提供将唤醒服务包含PDU字节系统SmsManager将读取该文件,确保发送上线。

  1. Create the Mms send-request - new SendReq() and config its date, body, to, etc.
  2. Create the Mms body - new PduBody().
  3. Create Parts via new PduPart() for each attachment, and add to the body: body.addPart(pdu)
  4. Add the body to the request - req.setBody(body)
  5. Convert the send-request to a byte[] ready to be sent by calling new PduComposer(context, mySendReq).make() - note that you'll need to copy lots of code from Android's source code to get the PduComposer class.
  6. Now's the interesting part - you save the byte[] to a local file accessible to your app only, and add ContentProvider class that allows other apps to request access to your file, this is MmsFileProvider class in the sample app, don't forget to declare your provider in your manifest file.
  7. Now, when you call the SmsManager.sendMultimediaMessage api, your file provider will wake up to serve the file containing the pdu bytes to the system SmsManager that will read it and send it on the wire.

有了这样说,此API只为我工作在某些设备(如Nexuses),但不能在其他一些国家(例如HTC一)。
看到这里我的SO问题:
SmsManager彩信的API上HTC

Having that said, this API is only working for me on some devices (e.g. Nexuses), but not on some others (e.g. HTC One). See my SO question here: SmsManager MMS APIs on HTC

这篇关于无法发送用MMS SmsManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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