最近有个需求,不去调用系统界面发送彩信功能。做过发送短信功能的同学可能第一反应是这样:
不使用 StartActivity,像发短信那样,调用一个类似于发短信的方法
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneCode, null, text, null, null);
可以实现吗? 答案是否定的,因为Android上根本就没有提供发送彩信的接口,如果你想发送彩信,对不起,请调用系统彩信app界面,如下:
Java代码
1. Intent sendIntent = new Intent(Intent.ACTION_SEND, Uri.parse("mms://"));
2.sendIntent.setType("image/jpeg");
3.String url = "file://sdcard//tmpPhoto.jpg";
4.sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
5.startActivity(Intent.createChooser(sendIntent, "MMS:"));
但是这种方法往往不能满足我们的需求,能不能不调用系统界面,自己实现发送彩信呢?经过几天的努力,终于找到了解决办法。
第一步:先构造出你要发送的彩信内容,即构建一个pdu,需要用到以下几个类,这些类都是从Android源码的MMS应用中mms.pdu包中copy出来的。你需要将pdu包中的所有类
都拷贝到你的工程中,然后自己酌情调通。
Java代码 1. final SendReq sendRequest = new SendReq();
2. final PduBody pduBody = new PduBody();
3.final PduPart part = new PduPart();//存放附件,每个附件是一个part,如果添加多个附件,就想body中add多个part。
4.
5. pduBody.addPart(partPdu);
6. sendRequest.setBody(pduBody);
7. final PduComposer composer = new PduComposer(ctx, sendRequest);
8.final byte[] bytesToSend = composer.make(); //将彩信的内容以及主题等信息转化成byte数组,准备通过http协议//发送到 ”http://mmsc.monternet.com”;
第二步:发送彩信到彩信中心。
构建pdu的代码:
Java代码 1. String subject = "测试彩信";
2. String recipient = "接收彩信的号码";//138xxxxxxx
3. final SendReq sendRequest = new SendReq();
4. final EncodedStringValue[] sub = EncodedStringValue.extract(subject);
5. if (sub != null && sub.length > 0) {
6. sendRequest.setSubject(sub[0]);
7. }
8. final EncodedStringValue[] phoneNumbers = EncodedStringValue.extract(recipient);
9. if (phoneNumbers != null && phoneNumbers.length > 0) {
10. sendRequest.addTo(phoneNumbers[0]);
11. }
12. final PduBody pduBody = new PduBody();
13. final PduPart part = new PduPart();
14. part.setName("sample".getBytes());
15. part.setContentType("image/png".getBytes());
16. String furl = "file://mnt/sdcard//1.jpg";
17.
18. final PduPart partPdu = new PduPart();
19. partPdu.setCharset(CharacterSets.UTF_8);//UTF_16
20. partPdu.setName(part.getName());
21. partPdu.setContentType(part.getContentType());
22. partPdu.setDataUri(Uri.parse(furl));
23. pduBody.addPart(partPdu);
24.
25. sendRequest.setBody(pduBody);
26. final PduComposer composer = new PduComposer(ctx, sendRequest);
27. final byte[] bytesToSend = composer.make();
28.
29. Thread t = new Thread(new Runnable() {
30.
31. @Override
32. public void run() {
33. try {
34. HttpConnectInterface.sendMMS(ctx, bytesToSend);
35.//
36. } catch (IOException e) {
37. e.printStackTrace();
38. }
39. }
40. });
41. t.start();
42.发送pdu到彩信中心的代码:
43. public static String mmscUrl = "http://mmsc.monternet.com";
44.// public static String mmscUrl = "http://www.baidu.com/";
45. public static String mmsProxy = "10.0.0.172";
46. public static String mmsProt = "80";
47.
48. private static String HDR_VALUE_ACCEPT_LANGUAGE = "";
49. // Definition for necessary HTTP headers.
50. private static final String HDR_KEY_ACCEPT = "Accept";
51. private static final String HDR_KEY_ACCEPT_LANGUAGE = "Accept-Language";
52.
53. private static final String HDR_VALUE_ACCEPT =
54. "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic";
55.public static byte[] sendMMS(Context context, byte[] pdu)throws IOException{
56. HDR_VALUE_ACCEPT_LANGUAGE = getHttpAcceptLanguage();
57.
58. if (mmscUrl == null) {
59. throw new IllegalArgumentException("URL must not be null.");
60. }
61.
62. HttpClient client = null;
63. try {
64. // Make sure to use a proxy which supports CONNECT.
65. client = HttpConnector.buileClient(context);
66. HttpPost post = new HttpPost(mmscUrl);
67. //mms PUD START
68. ByteArrayEntity entity = new ByteArrayEntity(pdu);
69. entity.setContentType("application/vnd.wap.mms-message");
70. post.setEntity(entity);
71. post.addHeader(HDR_KEY_ACCEPT, HDR_VALUE_ACCEPT);
72. post.addHeader(HDR_KEY_ACCEPT_LANGUAGE, HDR_VALUE_ACCEPT_LANGUAGE);
73. //mms PUD END
74. HttpParams params = client.getParams();
75. HttpProtocolParams.setContentCharset(params, "UTF-8");
76. HttpResponse response = client.execute(post);
77.
78. LogUtility.showLog(tag, "111");
79. StatusLine status = response.getStatusLine();
80. LogUtility.showLog(tag, "status "+status.getStatusCode());
81. if (status.getStatusCode() != 200) { // HTTP 200 is not success.
82. LogUtility.showLog(tag, "!200");
83. throw new IOException("HTTP error: " + status.getReasonPhrase());
84. }
85. HttpEntity resentity = response.getEntity();
86. byte[] body = null;
87. if (resentity != null) {
88. try {
89. if (resentity.getContentLength() > 0) {
90. body = new byte[(int) resentity.getContentLength()];
91. DataInputStream dis = new DataInputStream(resentity.getContent());
92. try {
93. dis.readFully(body);
94. } finally {
95. try {
96. dis.close();
97. } catch (IOException e) {
98. Log.e(tag, "Error closing input stream: " + e.getMessage());
99. }
100. }
101. }
102. } finally {
103. if (entity != null) {
104. entity.consumeContent();
105. }
106. }
107. }
108. LogUtility.showLog(tag, "result:"+new String(body));
109. return body;
110. } catch (IllegalStateException e) {
111. LogUtility.showLog(tag, "",e);
112.// handleHttpConnectionException(e, mmscUrl);
113. } catch (IllegalArgumentException e) {
114. LogUtility.showLog(tag, "",e);
115.// handleHttpConnectionException(e, mmscUrl);
116. } catch (SocketException e) {
117. LogUtility.showLog(tag, "",e);
118.// handleHttpConnectionException(e, mmscUrl);
119. } catch (Exception e) {
120. LogUtility.showLog(tag, "",e);
121. //handleHttpConnectionException(e, mmscUrl);
122. } finally {
123. if (client != null) {
124.// client.;
125. }
126. }
127. return new byte[0];
128. }
至此,彩信的发送算是完成了。
总结:Android的彩信相关操作都是没有api的,包括彩信的读取、发送、存储。这些过程都是需要手动去完成的。想要弄懂这些过程,需要仔细阅读android源码中的mms这个app。还有就是去研究mmssms.db数据库,因为彩信的读取和存储其实都是对mmssms.db这个数据库的操作过程。而且因为这个是共享的数据库,所以只能用ContentProvider这个组件去操作db。
总之,想要研究彩信这块(包括普通短信),你就必须的研究mmssms.db的操作方法,多多了解每个表对应的哪个uri,每个uri能提供什么样的操作,那些字段代表短信的那些属性等。
最后推荐个好用的sqlite查看工具:SQLite Database Browser。
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011-08/41609.htm
不使用 StartActivity,像发短信那样,调用一个类似于发短信的方法
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneCode, null, text, null, null);
可以实现吗? 答案是否定的,因为Android上根本就没有提供发送彩信的接口,如果你想发送彩信,对不起,请调用系统彩信app界面,如下:
Java代码
1. Intent sendIntent = new Intent(Intent.ACTION_SEND, Uri.parse("mms://"));
2.sendIntent.setType("image/jpeg");
3.String url = "file://sdcard//tmpPhoto.jpg";
4.sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
5.startActivity(Intent.createChooser(sendIntent, "MMS:"));
但是这种方法往往不能满足我们的需求,能不能不调用系统界面,自己实现发送彩信呢?经过几天的努力,终于找到了解决办法。
第一步:先构造出你要发送的彩信内容,即构建一个pdu,需要用到以下几个类,这些类都是从Android源码的MMS应用中mms.pdu包中copy出来的。你需要将pdu包中的所有类
都拷贝到你的工程中,然后自己酌情调通。
Java代码 1. final SendReq sendRequest = new SendReq();
2. final PduBody pduBody = new PduBody();
3.final PduPart part = new PduPart();//存放附件,每个附件是一个part,如果添加多个附件,就想body中add多个part。
4.
5. pduBody.addPart(partPdu);
6. sendRequest.setBody(pduBody);
7. final PduComposer composer = new PduComposer(ctx, sendRequest);
8.final byte[] bytesToSend = composer.make(); //将彩信的内容以及主题等信息转化成byte数组,准备通过http协议//发送到 ”http://mmsc.monternet.com”;
第二步:发送彩信到彩信中心。
构建pdu的代码:
Java代码 1. String subject = "测试彩信";
2. String recipient = "接收彩信的号码";//138xxxxxxx
3. final SendReq sendRequest = new SendReq();
4. final EncodedStringValue[] sub = EncodedStringValue.extract(subject);
5. if (sub != null && sub.length > 0) {
6. sendRequest.setSubject(sub[0]);
7. }
8. final EncodedStringValue[] phoneNumbers = EncodedStringValue.extract(recipient);
9. if (phoneNumbers != null && phoneNumbers.length > 0) {
10. sendRequest.addTo(phoneNumbers[0]);
11. }
12. final PduBody pduBody = new PduBody();
13. final PduPart part = new PduPart();
14. part.setName("sample".getBytes());
15. part.setContentType("image/png".getBytes());
16. String furl = "file://mnt/sdcard//1.jpg";
17.
18. final PduPart partPdu = new PduPart();
19. partPdu.setCharset(CharacterSets.UTF_8);//UTF_16
20. partPdu.setName(part.getName());
21. partPdu.setContentType(part.getContentType());
22. partPdu.setDataUri(Uri.parse(furl));
23. pduBody.addPart(partPdu);
24.
25. sendRequest.setBody(pduBody);
26. final PduComposer composer = new PduComposer(ctx, sendRequest);
27. final byte[] bytesToSend = composer.make();
28.
29. Thread t = new Thread(new Runnable() {
30.
31. @Override
32. public void run() {
33. try {
34. HttpConnectInterface.sendMMS(ctx, bytesToSend);
35.//
36. } catch (IOException e) {
37. e.printStackTrace();
38. }
39. }
40. });
41. t.start();
42.发送pdu到彩信中心的代码:
43. public static String mmscUrl = "http://mmsc.monternet.com";
44.// public static String mmscUrl = "http://www.baidu.com/";
45. public static String mmsProxy = "10.0.0.172";
46. public static String mmsProt = "80";
47.
48. private static String HDR_VALUE_ACCEPT_LANGUAGE = "";
49. // Definition for necessary HTTP headers.
50. private static final String HDR_KEY_ACCEPT = "Accept";
51. private static final String HDR_KEY_ACCEPT_LANGUAGE = "Accept-Language";
52.
53. private static final String HDR_VALUE_ACCEPT =
54. "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic";
55.public static byte[] sendMMS(Context context, byte[] pdu)throws IOException{
56. HDR_VALUE_ACCEPT_LANGUAGE = getHttpAcceptLanguage();
57.
58. if (mmscUrl == null) {
59. throw new IllegalArgumentException("URL must not be null.");
60. }
61.
62. HttpClient client = null;
63. try {
64. // Make sure to use a proxy which supports CONNECT.
65. client = HttpConnector.buileClient(context);
66. HttpPost post = new HttpPost(mmscUrl);
67. //mms PUD START
68. ByteArrayEntity entity = new ByteArrayEntity(pdu);
69. entity.setContentType("application/vnd.wap.mms-message");
70. post.setEntity(entity);
71. post.addHeader(HDR_KEY_ACCEPT, HDR_VALUE_ACCEPT);
72. post.addHeader(HDR_KEY_ACCEPT_LANGUAGE, HDR_VALUE_ACCEPT_LANGUAGE);
73. //mms PUD END
74. HttpParams params = client.getParams();
75. HttpProtocolParams.setContentCharset(params, "UTF-8");
76. HttpResponse response = client.execute(post);
77.
78. LogUtility.showLog(tag, "111");
79. StatusLine status = response.getStatusLine();
80. LogUtility.showLog(tag, "status "+status.getStatusCode());
81. if (status.getStatusCode() != 200) { // HTTP 200 is not success.
82. LogUtility.showLog(tag, "!200");
83. throw new IOException("HTTP error: " + status.getReasonPhrase());
84. }
85. HttpEntity resentity = response.getEntity();
86. byte[] body = null;
87. if (resentity != null) {
88. try {
89. if (resentity.getContentLength() > 0) {
90. body = new byte[(int) resentity.getContentLength()];
91. DataInputStream dis = new DataInputStream(resentity.getContent());
92. try {
93. dis.readFully(body);
94. } finally {
95. try {
96. dis.close();
97. } catch (IOException e) {
98. Log.e(tag, "Error closing input stream: " + e.getMessage());
99. }
100. }
101. }
102. } finally {
103. if (entity != null) {
104. entity.consumeContent();
105. }
106. }
107. }
108. LogUtility.showLog(tag, "result:"+new String(body));
109. return body;
110. } catch (IllegalStateException e) {
111. LogUtility.showLog(tag, "",e);
112.// handleHttpConnectionException(e, mmscUrl);
113. } catch (IllegalArgumentException e) {
114. LogUtility.showLog(tag, "",e);
115.// handleHttpConnectionException(e, mmscUrl);
116. } catch (SocketException e) {
117. LogUtility.showLog(tag, "",e);
118.// handleHttpConnectionException(e, mmscUrl);
119. } catch (Exception e) {
120. LogUtility.showLog(tag, "",e);
121. //handleHttpConnectionException(e, mmscUrl);
122. } finally {
123. if (client != null) {
124.// client.;
125. }
126. }
127. return new byte[0];
128. }
至此,彩信的发送算是完成了。
总结:Android的彩信相关操作都是没有api的,包括彩信的读取、发送、存储。这些过程都是需要手动去完成的。想要弄懂这些过程,需要仔细阅读android源码中的mms这个app。还有就是去研究mmssms.db数据库,因为彩信的读取和存储其实都是对mmssms.db这个数据库的操作过程。而且因为这个是共享的数据库,所以只能用ContentProvider这个组件去操作db。
总之,想要研究彩信这块(包括普通短信),你就必须的研究mmssms.db的操作方法,多多了解每个表对应的哪个uri,每个uri能提供什么样的操作,那些字段代表短信的那些属性等。
最后推荐个好用的sqlite查看工具:SQLite Database Browser。
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011-08/41609.htm