Android短信功能实现源码

一段完整的Android平台上短信功能的接口源码,利用扩展的API可以通过js实现如下功能:
1. getContentUris():读取短信相关的所有数据库表的Uri地址;
2. get(int number):读取若干条短信;
3. getUnread(int number):读取若干条未读短信;
4. getRead(int number):读取若干条已读短信;
5. getInbox(int number):从收件箱读取若干条短信;
6. getSent(int number):读取若干条已发短信;
7. getByThread(int threadID):读取会话中所有短信;
8. getThreads(int number):读取若干条会话;
9. getData(String selection, int number):根据条件读取若干条短信。
代码] Android短信功能接口
001 /*
002 * Copyright (C) 2011 The Rexsee Open Source Project
003 *
004 * Licensed under the Rexsee License, Version 1.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.rexsee.com/CN/legal/license.html
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package rexsee.communication;
018
019 import rexsee.core.browser.JavascriptInterface;
020 import rexsee.core.browser.RexseeBrowser;
021 import rexsee.core.utilities.Escape;
022 import android.content.ContentResolver;
023 import android.content.Context;
024 import android.database.Cursor;
025 import android.net.Uri;
026
027 public class RexseeSMS implements JavascriptInterface {
028
029 private static final String INTERFACE_NAME = "SMS";
030 @Override
031 public String getInterfaceName() {
032 return mBrowser.application.resources.prefix + INTERFACE_NAME;
033 }
034 @Override
035 public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {
036 return this;
037 }
038 @Override
039 public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) {
040 return new RexseeSMS(childBrowser);
041 }
042
043 public static final String CONTENT_URI_SMS = "content://sms";
044 public static final String CONTENT_URI_SMS_INBOX = "content://sms/inbox";
045 public static final String CONTENT_URI_SMS_SENT = "content://sms/sent";
046 public static final String CONTENT_URI_SMS_CONVERSATIONS = "content://sms/conversations";
047
048 public static String[] SMS_COLUMNS = new String[]{
049 "_id", //0
050 "thread_id", //1
051 "address", //2
052 "person", //3
053 "date", //4
054 "body", //5
055 "read", //6; 0:not read 1:read; default is 0
056 "type", //7; 0:all 1:inBox 2:sent 3:draft 4:outBox 5:failed 6:queued
057 "service_center" //8
058 };
059 public static String[] THREAD_COLUMNS = new String[]{
060 "thread_id",
061 "msg_count",
062 "snippet",
063 };
064
065 private final Context mContext;
066 private final RexseeBrowser mBrowser;
067
068 public RexseeSMS(RexseeBrowser browser) {
069 mBrowser = browser;
070 mContext = browser.getContext();
071 }
072
073 //JavaScript Interface
074 public String getContentUris() {
075 String rtn = "{";
076 rtn += "\"sms\":\"" + CONTENT_URI_SMS + "\"";
077 rtn += ",\"inbox\":\"" + CONTENT_URI_SMS_INBOX + "\"";
078 rtn += ",\"sent\":\"" + CONTENT_URI_SMS_SENT + "\"";
079 rtn += ",\"conversations\":\"" + CONTENT_URI_SMS_CONVERSATIONS + "\"";
080 rtn += "}";
081 return rtn;
082 }
083
084 public String get(int number) {
085 return getData(null, number);
086 }
087 public String getUnread(int number) {
088 return getData("type=1 AND read=0", number);
089 }
090 public String getRead(int number) {
091 return getData("type=1 AND read=1", number);
092 }
093 public String getInbox(int number) {
094 return getData("type=1", number);
095 }
096 public String getSent(int number) {
097 return getData("type=2", number);
098 }
099 public String getByThread(int thread) {
100 return getData("thread_id=" + thread, 0);
101 }
102 public String getData(String selection, int number) {
103 Cursor cursor = null;
104 ContentResolver contentResolver = mContext.getContentResolver();
105 try {
106 if (number > 0) {
107 cursor = contentResolver.query(Uri.parse(CONTENT_URI_SMS), SMS_COLUMNS, selection, null, "date desc limit " + number);
108 } else {
109 cursor = contentResolver.query(Uri.parse(CONTENT_URI_SMS), SMS_COLUMNS, selection, null, "date desc");
110 }
111 if (cursor == null || cursor.getCount() == 0) return "[]";
112 String rtn = "";
113 for (int i = 0; i < cursor.getCount(); i++) {
114 cursor.moveToPosition(i);
115 if (i > 0) rtn += ",";
116 rtn += "{";
117 rtn += "\"_id\":" + cursor.getString(0);
118 rtn += ",\"thread_id\":" + cursor.getString(1);
119 rtn += ",\"address\":\"" + cursor.getString(2) + "\"";
120 rtn += ",\"person\":\"" + ((cursor.getString(3) == null) ? "" : cursor.getString(3)) + "\"";
121 rtn += ",\"date\":" + cursor.getString(4);
122 rtn += ",\"body\":\"" + Escape.escape(cursor.getString(5)) + "\"";
123 rtn += ",\"read\":" + ((cursor.getInt(6) == 1) ? "true" : "false");
124 rtn += ",\"type\":" + cursor.getString(7);
125 rtn += ",\"service_center\":" + cursor.getString(8);
126 rtn += "}";
127 }
128 return "[" + rtn + "]";
129 } catch (Exception e) {
130 mBrowser.exception(getInterfaceName(), e);
131 return "[]";
132 }
133 }
134 public String getThreads(int number) {
135 Cursor cursor = null;
136 ContentResolver contentResolver = mContext.getContentResolver();
137 try {
138 if (number > 0) {
139 cursor = contentResolver.query(Uri.parse(CONTENT_URI_SMS_CONVERSATIONS), THREAD_COLUMNS, null, null, "thread_id desc limit " + number);
140 } else {
141 cursor = contentResolver.query(Uri.parse(CONTENT_URI_SMS_CONVERSATIONS), THREAD_COLUMNS, null, null, "thread_id desc");
142 }
143 if (cursor == null || cursor.getCount() == 0) return "[]";
144 String rtn = "";
145 for (int i = 0; i < cursor.getCount(); i++) {
146 cursor.moveToPosition(i);
147 if (i > 0) rtn += ",";
148 rtn += "{";
149 rtn += "\"thread_id\":" + cursor.getString(0);
150 rtn += ",\"msg_count\":" + cursor.getString(1);
151 rtn += ",\"snippet\":\"" + Escape.escape(cursor.getString(2)) + "\"";
152 rtn += "}";
153 }
154 return "[" + rtn + "]";
155 } catch (Exception e) {
156 mBrowser.exception(getInterfaceName(), e);
157 return "[]";
158 }
159 }
160
161 }
原文地址:https://www.cnblogs.com/tuncaysanli/p/2469066.html