Android 彩信的发送最近有个需求,不去调用系统界面发送彩信功能。做过发送短信功能的同学可能第一反应是这样:不使用 StartActivity,像发短信那样,调用一个类似于发短信的方法SmsManager smsManager = SmsManager.getDefault();smsManager.sendTextMessage(phoneCode, null, text, null, null);可以实现吗? 答案是否定的,因为android上根本就没有提供发送彩信的接口,如果你想发送彩信,对不起,请调用系统彩信app界面,如下            Intent sendIntent = new Intent(Intent.ACTION_SEND,  Uri.parse("mms://"));    sendIntent.setType("image/jpeg");    String url = "file://sdcard//tmpPhoto.jpg";    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));    startActivity(Intent.createChooser(sendIntent, "MMS:"));但是这种方法往往不能满足我们的需求,能不能不调用系统界面,自己实现发送彩信呢?经过几天的努力,终于找到了解决办法。第一步:先构造出你要发送的彩信内容,即构建一个pdu,需要用到以下几个类,这些类都是从android源码的MMS应用中mms.pdu包中copy出来的。你需要将pdu包中的所有类都拷贝到你的工程中,然后自己酌情调通。   final SendReq sendRequest = new SendReq();   final PduBody pduBody = new PduBody();final PduPart part = new PduPart();//存放附件,每个附件是一个part,如果添加多个附件,就想body中add多个part。   pduBody.addPart(partPdu);   sendRequest.setBody(pduBody);   final PduComposer composer = new PduComposer(ctx, sendRequest);final byte[] bytesToSend = composer.make(); //将彩信的内容以及主题等信息转化成byte数组,准备通过http协议发送到 ”http://mmsc.monternet.com”; 第二步:发送彩信到彩信中心。 构建pdu的代码:  String subject = "测试彩信";    String recipient = "接收彩信的号码";//138xxxxxxx    final SendReq sendRequest = new SendReq();    final EncodedStringValue[] sub = EncodedStringValue.extract(subject);    if (sub != null && sub.length > 0) {    sendRequest.setSubject(sub[0]);    }    final EncodedStringValue[] phoneNumbers = EncodedStringValue.extract(recipient);    if (phoneNumbers != null && phoneNumbers.length > 0) {    sendRequest.addTo(phoneNumbers[0]);    }    final PduBody pduBody = new PduBody();    final PduPart part = new PduPart();    part.setName("sample".getBytes());    part.setContentType("image/png".getBytes());    String furl = "file://mnt/sdcard//1.jpg";     final PduPart partPdu = new PduPart();    partPdu.setCharset(CharacterSets.UTF_8);//UTF_16    partPdu.setName(part.getName());    partPdu.setContentType(part.getContentType());    partPdu.setDataUri(Uri.parse(furl));    pduBody.addPart(partPdu);        sendRequest.setBody(pduBody);    final PduComposer composer = new PduComposer(ctx, sendRequest);    final byte[] bytesToSend = composer.make();     Thread t = new Thread(new Runnable() { @Overridepublic void run() {try {HttpConnectInterface.sendMMS(ctx,  bytesToSend);//} catch (IOException e) {e.printStackTrace();}}});    t.start();发送pdu到彩信中心的代码:        public static String mmscUrl = "http://mmsc.monternet.com";//public static String mmscUrl = "http://www.baidu.com/";public static String mmsProxy = "10.0.0.172";public static String mmsProt = "80";        private static String HDR_VALUE_ACCEPT_LANGUAGE = "";    // Definition for necessary HTTP headers.       private static final String HDR_KEY_ACCEPT = "Accept";       private static final String HDR_KEY_ACCEPT_LANGUAGE = "Accept-Language";     private static final String HDR_VALUE_ACCEPT =        "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic";public static byte[] sendMMS(Context context, byte[] pdu)throws IOException{HDR_VALUE_ACCEPT_LANGUAGE = getHttpAcceptLanguage(); if (mmscUrl == null) {            throw new IllegalArgumentException("URL must not be null.");        }         HttpClient client = null;        try {        // Make sure to use a proxy which supports CONNECT.        client = HttpConnector.buileClient(context);        HttpPost post = new HttpPost(mmscUrl);        //mms PUD START        ByteArrayEntity entity = new ByteArrayEntity(pdu);entity.setContentType("application/vnd.wap.mms-message");        post.setEntity(entity);        post.addHeader(HDR_KEY_ACCEPT, HDR_VALUE_ACCEPT);        post.addHeader(HDR_KEY_ACCEPT_LANGUAGE, HDR_VALUE_ACCEPT_LANGUAGE);        //mms PUD END        HttpParams params = client.getParams();        HttpProtocolParams.setContentCharset(params, "UTF-8");        HttpResponse response = client.execute(post); LogUtility.showLog(tag, "111");        StatusLine status = response.getStatusLine();        LogUtility.showLog(tag, "status "+status.getStatusCode());        if (status.getStatusCode() != 200) { // HTTP 200 is not success.            LogUtility.showLog(tag, "!200");                throw new IOException("HTTP error: " + status.getReasonPhrase());            }        HttpEntity resentity = response.getEntity();            byte[] body = null;            if (resentity != null) {                try {                    if (resentity.getContentLength() > 0) {                        body = new byte[(int) resentity.getContentLength()];                        DataInputStream dis = new DataInputStream(resentity.getContent());                        try {                            dis.readFully(body);                        } finally {                            try {                                dis.close();                            } catch (IOException e) {                                Log.e(tag, "Error closing input stream: " + e.getMessage());                            }                        }                    }                } finally {                    if (entity != null) {                        entity.consumeContent();                    }                }            }            LogUtility.showLog(tag, "result:"+new String(body));            return body;}  catch (IllegalStateException e) {LogUtility.showLog(tag, "",e);//            handleHttpConnectionException(e, mmscUrl);        } catch (IllegalArgumentException e) {        LogUtility.showLog(tag, "",e);//            handleHttpConnectionException(e, mmscUrl);        } catch (SocketException e) {        LogUtility.showLog(tag, "",e);//            handleHttpConnectionException(e, mmscUrl);        } catch (Exception e) {        LogUtility.showLog(tag, "",e);        //handleHttpConnectionException(e, mmscUrl);        } finally {            if (client != null) {//                client.;            }        }return new byte[0];}至此,彩信的发送算是完成了。总结:android的彩信相关操作都是没有api的,包括彩信的读取、发送、存储。这些过程都是需要手动去完成的。想要弄懂这些过程,需要仔细阅读android源码中的mms这个app。还有就是去研究mmssms.db数据库,因为彩信的读取和存储其实都是对mmssms.db这个数据库的操作过程。而且因为这个是共享的数据库,所以只能用ContentProvider这个组件去操作db。总之,想要研究彩信这块(包括普通短信),你就必须的研究mmssms.db的操作方法,多多了解每个表对应的哪个uri,每个uri能提供什么样的操作,那些字段代表短信的那些属性等。最后推荐个好用的sqlite查看工具:SQLite Database Browser。

更多相关文章

  1. Android实现KSOAP2访问WebService
  2. Android学习-HelloWorldAndroid
  3. Android(安卓)RecyclerView聊天界面控件布局居底
  4. Android(安卓)短信 彩信 wap push的接收
  5. android  打开多个Activity,返回到第一个Activity的问题
  6. Android开发之侧拉栏的使用
  7. Android下载完成更新后,没有打开安装成功界面,出现闪退问题
  8. Android(安卓)之 调用短信界面
  9. android学习记录(三) UI界面

随机推荐

  1. 如何查看模拟器上的sqlite数据库
  2. [Android]通过PhoneLookup读取所有电话号
  3. Android(安卓)Dalvikvm 内存管理理解
  4. android 调用 webservice
  5. Android(安卓)Canvas 绘图
  6. SpannableStringBuilder 和 SpannableStr
  7. android 系统数据操作说明
  8. 《宅男的android开发指南》(翻译)--5
  9. Android进程间通信机制——基础篇
  10. Android微信分享,无响应