Rexsee API介绍:SMS短信读取

Rexsee是一个开源的移动Web开发平台,深度支持Android。目前已扩展了接近2000个API,可以通过js直接实现Android原生功能。陆续梳理相关的对象与事件说明,并将源码贴出来。有兴趣的TX也可以在Rexsee的社区直接查看详细内容: http://www.rexsee.com/CN/helpReference.php


基于rexsee扩展的rexseeSMS,通过JS实现短信读取相关功能,如:
【函数】 JsonArray getContentUris()
【说明】 读取短信相关的所有数据库表的Uri地址。
【返回】 Json对象字符串,可以用eval('('+json+')')转换为JavaScript对象。
【参数】
【示例】
alert(rexseeSMS.getContentUris());

【函数】 JsonObjectArray get(int number)
【说明】 读取若干条短信。
【返回】 Json对象数组字符串,可以用eval('('+json+')')转换为JavaScript对象数组,注意,其中body字段是escape过的,使用前需要unescape。
【参数】 number:要读取的短信条数。
【示例】
alert(rexseeSMS.get(10));
alert(unescape(eval('('+rexseeSMS.get(1)+')')[0].body));

其它还有:
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):根据条件读取若干条短信。


rexseeSMS.java详细源码:

/* 
* Copyright (C) 2011 The Rexsee Open Source Project 
* 
* Licensed under the Rexsee License, Version 1.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*      http://www.rexsee.com/CN/legal/license.html 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 
 
package rexsee.communication;  
 
import rexsee.core.browser.JavascriptInterface;  
import rexsee.core.browser.RexseeBrowser;  
import rexsee.core.utilities.Escape;  
import android.content.ContentResolver;  
import android.content.Context;  
import android.database.Cursor;  
import android.net.Uri;  
 
public class RexseeSMS implements JavascriptInterface {  
 
       private static final String INTERFACE_NAME = "SMS";  
       @Override  
       public String getInterfaceName() {  
               return mBrowser.application.resources.prefix + INTERFACE_NAME;  
       }  
       @Override  
       public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {  
               return this;  
       }  
       @Override  
       public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) {  
               return new RexseeSMS(childBrowser);  
       }  
 
       public static final String CONTENT_URI_SMS = "content://sms";  
       public static final String CONTENT_URI_SMS_INBOX = "content://sms/inbox";  
       public static final String CONTENT_URI_SMS_SENT = "content://sms/sent";  
       public static final String CONTENT_URI_SMS_CONVERSATIONS = "content://sms/conversations";  
 
       public static String[] SMS_COLUMNS = new String[]{  
                       "_id", //0   
                       "thread_id", //1   
                       "address", //2  
                       "person", //3  
                       "date", //4  
                       "body", //5  
                       "read", //6; 0:not read 1:read; default is 0  
                       "type", //7;  0:all 1:inBox 2:sent 3:draft 4:outBox  5:failed 6:queued  
                       "service_center" //8  
       };  
       public static String[] THREAD_COLUMNS = new String[]{  
                       "thread_id",  
                       "msg_count",  
                       "snippet",  
       };  
 
       private final Context mContext;  
       private final RexseeBrowser mBrowser;  
 
       public RexseeSMS(RexseeBrowser browser) {  
               mBrowser = browser;  
               mContext = browser.getContext();  
       }  
 
       //JavaScript Interface  
       public String getContentUris() {  
               String rtn = "{";  
               rtn += "\"sms\":\"" + CONTENT_URI_SMS + "\"";  
               rtn += ",\"inbox\":\"" + CONTENT_URI_SMS_INBOX + "\"";  
               rtn += ",\"sent\":\"" + CONTENT_URI_SMS_SENT + "\"";  
               rtn += ",\"conversations\":\"" + CONTENT_URI_SMS_CONVERSATIONS + "\"";  
               rtn += "}";  
               return rtn;  
       }  
 
       public String get(int number) {  
               return getData(null, number);  
       }  
       public String getUnread(int number) {  
               return getData("type=1 AND read=0", number);  
       }  
       public String getRead(int number) {  
               return getData("type=1 AND read=1", number);  
       }  
       public String getInbox(int number) {  
               return getData("type=1", number);  
       }  
       public String getSent(int number) {  
               return getData("type=2", number);  
       }  
       public String getByThread(int thread) {  
               return getData("thread_id=" + thread, 0);  
       }  
       public String getData(String selection, int number) {  
               Cursor cursor = null;  
               ContentResolver contentResolver = mContext.getContentResolver();  
               try {  
                       if (number > 0) {  
                               cursor = contentResolver.query(Uri.parse(CONTENT_URI_SMS), SMS_COLUMNS, selection, null, "date desc limit " + number);  
                       } else {  
                               cursor = contentResolver.query(Uri.parse(CONTENT_URI_SMS), SMS_COLUMNS, selection, null, "date desc");  
                       }  
                       if (cursor == null || cursor.getCount() == 0) return "[]";  
                       String rtn = "";  
                       for (int i = 0; i < cursor.getCount(); i++) {  
                               cursor.moveToPosition(i);  
                               if (i > 0) rtn += ",";  
                               rtn += "{";  
                               rtn += "\"_id\":" + cursor.getString(0);  
                               rtn += ",\"thread_id\":" + cursor.getString(1);  
                               rtn += ",\"address\":\"" + cursor.getString(2) + "\"";  
                               rtn += ",\"person\":\"" + ((cursor.getString(3) == null) ? "" : cursor.getString(3)) + "\"";  
                               rtn += ",\"date\":" + cursor.getString(4);  
                               rtn += ",\"body\":\"" + Escape.escape(cursor.getString(5)) + "\"";  
                               rtn += ",\"read\":" + ((cursor.getInt(6) == 1) ? "true" : "false");  
                               rtn += ",\"type\":" + cursor.getString(7);  
                               rtn += ",\"service_center\":" + cursor.getString(8);  
                               rtn += "}";  
                       }  
                       return "[" + rtn + "]";  
               } catch (Exception e) {  
                       mBrowser.exception(getInterfaceName(), e);  
                       return "[]";  
               }  
       }  
       public String getThreads(int number) {  
               Cursor cursor = null;  
               ContentResolver contentResolver = mContext.getContentResolver();  
               try {  
                       if (number > 0) {  
                               cursor = contentResolver.query(Uri.parse(CONTENT_URI_SMS_CONVERSATIONS), THREAD_COLUMNS, null, null, "thread_id desc limit " + number);  
                       } else {  
                               cursor = contentResolver.query(Uri.parse(CONTENT_URI_SMS_CONVERSATIONS), THREAD_COLUMNS, null, null, "thread_id desc");  
                       }  
                       if (cursor == null || cursor.getCount() == 0) return "[]";  
                       String rtn = "";  
                       for (int i = 0; i < cursor.getCount(); i++) {  
                               cursor.moveToPosition(i);  
                               if (i > 0) rtn += ",";  
                               rtn += "{";  
                               rtn += "\"thread_id\":" + cursor.getString(0);  
                               rtn += ",\"msg_count\":" + cursor.getString(1);  
                               rtn += ",\"snippet\":\"" + Escape.escape(cursor.getString(2)) + "\"";  
                               rtn += "}";  
                       }  
                       return "[" + rtn + "]";  
               } catch (Exception e) {  
                       mBrowser.exception(getInterfaceName(), e);  
                       return "[]";  
               }  
       }  
 
} 

仅对Rexsee的源码和函数事件做了整理,相关的demo或源码解析可以在Rexsee社区了解,目前Rexsee已提供了近2000个扩展,覆盖Android原生功能超过90%,且全部开放: http://www.rexsee.com/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值