/** * 从uri中获取SmsInfo对象数组 * * @param uri * @param count * @return */ private JSArray getSmsInfoFromUri(Uri uri, int count) { boolean limit = count > 0; // count大于0,则加上limit JSArray smsArray = new JSArray(); ContentResolver resolver = context.getContentResolver(); Cursor cursor = resolver.query( uri, null, null, null, Telephony.Sms.DEFAULT_SORT_ORDER + (limit ? " limit 0," + count : "")); int idIndex = cursor.getColumnIndexOrThrow(Telephony.Sms._ID); int addressIndex = cursor.getColumnIndexOrThrow(Telephony.Sms.ADDRESS); int dateIndex = cursor.getColumnIndexOrThrow(Telephony.Sms.DATE); int pidIndex = cursor.getColumnIndexOrThrow(Telephony.Sms.PERSON_ID); int bodyIndex = cursor.getColumnIndexOrThrow(Telephony.Sms.BODY); while (cursor.moveToNext()) { long id = cursor.getLong(idIndex); String address = cursor.getString(addressIndex); long pid = cursor.getLong(pidIndex); long date = cursor.getLong(dateIndex); String body = cursor.getString(bodyIndex); SmsInfo sms = new SmsInfo(body, date, address, id, pid); smsArray.add(sms); } return smsArray; }
/** * 获取count条未读短消息(有bug,获取的都是已读的) * * @param count * @return */ public JSArray getUnreadSms(int count) { boolean limit = count > 0; // count大于0,则加上limit JSArray smsArray = new JSArray(); ContentResolver resolver = context.getContentResolver(); // 查询短消息详细信息,按日期降序排列 Cursor cursor = resolver.query( Telephony.Sms.Inbox.CONTENT_URI, null, Telephony.Sms.Inbox.READ + "=?", new String[] {"0"}, Telephony.Sms.DEFAULT_SORT_ORDER + (limit ? " limit 0," + count : "")); int idIndex = cursor.getColumnIndexOrThrow(Telephony.Sms._ID); int addressIndex = cursor.getColumnIndexOrThrow(Telephony.Sms.ADDRESS); int dateIndex = cursor.getColumnIndexOrThrow(Telephony.Sms.DATE); int pidIndex = cursor.getColumnIndexOrThrow(Telephony.Sms.PERSON_ID); int bodyIndex = cursor.getColumnIndexOrThrow(Telephony.Sms.BODY); while (cursor.moveToNext()) { long id = cursor.getLong(idIndex); String address = cursor.getString(addressIndex); long pid = cursor.getLong(pidIndex); long date = cursor.getLong(dateIndex); String body = cursor.getString(bodyIndex); SmsInfo sms = new SmsInfo(body, date, address, id, pid); smsArray.add(sms); } return smsArray; }