一段完整的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):根据条件读取若干条短信。

1.[代码]Android短信功能接口跳至[1][全屏预览]

view source print ?
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 packagerexsee.communication;
018
019 importrexsee.core.browser.JavascriptInterface;
020 importrexsee.core.browser.RexseeBrowser;
021 importrexsee.core.utilities.Escape;
022 importandroid.content.ContentResolver;
023 importandroid.content.Context;
024 importandroid.database.Cursor;
025 importandroid.net.Uri;
026
027 publicclassRexseeSMSimplementsJavascriptInterface {
028
029 privatestaticfinalString INTERFACE_NAME ="SMS";
030 @Override
031 publicString getInterfaceName() {
032 returnmBrowser.application.resources.prefix + INTERFACE_NAME;
033 }
034 @Override
035 publicJavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {
036 returnthis;
037 }
038 @Override
039 publicJavascriptInterface getNewInterface(RexseeBrowser childBrowser) {
040 returnnewRexseeSMS(childBrowser);
041 }
042
043 publicstaticfinalString CONTENT_URI_SMS ="content://sms";
044 publicstaticfinalString CONTENT_URI_SMS_INBOX ="content://sms/inbox";
045 publicstaticfinalString CONTENT_URI_SMS_SENT ="content://sms/sent";
046 publicstaticfinalString CONTENT_URI_SMS_CONVERSATIONS ="content://sms/conversations";
047
048 publicstaticString[] SMS_COLUMNS =newString[]{
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 publicstaticString[] THREAD_COLUMNS =newString[]{
060 "thread_id",
061 "msg_count",
062 "snippet",
063 };
064
065 privatefinalContext mContext;
066 privatefinalRexseeBrowser mBrowser;
067
068 publicRexseeSMS(RexseeBrowser browser) {
069 mBrowser = browser;
070 mContext = browser.getContext();
071 }
072
073 //JavaScript Interface
074 publicString 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 returnrtn;
082 }
083
084 publicString get(intnumber) {
085 returngetData(null, number);
086 }
087 publicString getUnread(intnumber) {
088 returngetData("type=1 AND read=0", number);
089 }
090 publicString getRead(intnumber) {
091 returngetData("type=1 AND read=1", number);
092 }
093 publicString getInbox(intnumber) {
094 returngetData("type=1", number);
095 }
096 publicString getSent(intnumber) {
097 returngetData("type=2", number);
098 }
099 publicString getByThread(intthread) {
100 returngetData("thread_id="+ thread,0);
101 }
102 publicString getData(String selection,intnumber) {
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(inti =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 publicString getThreads(intnumber) {
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(inti =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 }

更多相关文章

  1. android发送短信例子
  2. libevent 在 Android(安卓)上的一个改进
  3. Android(安卓)Contacts(二)—— SMS 短信 与 Contacts 联系人关联
  4. Android(安卓)文件保存与读取
  5. 赵雅智_android短信窃听及android短信源码解析
  6. android往文件中保存和读取数据
  7. Android(安卓)C 语言读取系统属性
  8. Android(安卓)读写文件的N种写法
  9. android 读取json数据(遍历JSONObject和JSONArray)

随机推荐

  1. Android离线语音识别(SpeechRecognizer、P
  2. Android中保存和恢复Fragment状态的最好
  3. Android磁盘管理-之vold源码分析(2)
  4. Android中StringBuilder与 StringBuffer
  5. Android(安卓)需要动态申请的权限以及Eas
  6. Android之垂直显示TextView
  7. MVVM实现数据双向绑定
  8. Android-Fragment
  9. Android中Activity组件的生命周期
  10. Cocos2d-x Mac下环境搭建