您好,我正在实现一个 SMS 应用程序,现在能够检索所有消息及其各自的联系信息,如显示名称、照片 uri.. 并将它们显示在自定义列表中,点击项目将带您进入相应的讨论。我的问题是同步所有这些消息所花费的时间,
这是我的代码:
读取短信.java:
public class ReadSMS {
ArrayList<HashMap<Contact, ArrayList<OneComment>>> recentChats;
Application _context;
public ReadSMS(Application context) {
this._context = context;
this.recentChats = ((ChatApplication) _context).getChats();
}
public ArrayList<HashMap<Contact, ArrayList<OneComment>>> getSMS() {
// Init
ArrayList<SmsMsg> smsMsgs = new ArrayList<SmsMsg>();
TreeSet<Integer> threadIds = new TreeSet<Integer>();
Uri mSmsinboxQueryUri = Uri.parse("content://sms");
Cursor cursor = _context.getContentResolver().query(
mSmsinboxQueryUri,
new String[] { "_id", "thread_id", "address", "date", "body",
"type" }, null, null, null);
String[] columns = new String[] { "address", "thread_id", "date",
"body", "type" };
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
SmsMsg smsMsg = new SmsMsg();
String address = null, displayName = null, date = null, msg = null, type = null, threadId = null;
Uri photoUri = null;
threadId = cursor.getString(cursor.getColumnIndex(columns[1]));
type = cursor.getString(cursor.getColumnIndex(columns[4]));
if (Integer.parseInt(type) == 1 || Integer.parseInt(type) == 2) {
address = cursor.getString(cursor
.getColumnIndex(columns[0]));
if (address.length() > 0) {
String[] contactData = getContactByNumber(address);
if (contactData != null) {
displayName = contactData[0];
if (contactData[1] != null)
photoUri = Uri.parse(contactData[1]);
}
} else
address = null;
date = cursor.getString(cursor.getColumnIndex(columns[2]));
msg = cursor.getString(cursor.getColumnIndex(columns[3]));
smsMsg.setDisplayName(displayName);
smsMsg.setThreadId(threadId);
smsMsg.setAddress(address);
smsMsg.setPhotoUri(photoUri);
smsMsg.setDate(date);
smsMsg.setMsg(msg);
smsMsg.setType(type);
// Log.e("SMS-inbox", "\n\nNAME: " + displayName
// + "\nTHREAD_ID: " + threadId + "\nNUMBER: "
// + address + "\nPHOTO_URI: " + photoUri + "\nTIME: "
// + date + "\nMESSAGE: " + msg + "\nTYPE: " + type);
smsMsgs.add(smsMsg);
// Add threadId to Tree
threadIds.add(Integer.parseInt(threadId));
}
}
for (int threadId : threadIds) {
HashMap<Contact, ArrayList<OneComment>> oneChat = new HashMap<Contact, ArrayList<OneComment>>();
Contact con = new Contact();
ArrayList<OneComment> oneDisc = new ArrayList<OneComment>();
for (SmsMsg smsMsg : smsMsgs) {
if (Integer.parseInt(smsMsg.getThreadId()) == threadId) {
con.setContactName(smsMsg.getDisplayName());
con.setContactNumber(smsMsg.getAddress());
con.setContactPhotoUri(smsMsg.getPhotoUri());
if (Integer.parseInt(smsMsg.getType()) == 1)
oneDisc.add(0, new OneComment(true,
smsMsg.getMsg(), smsMsg.getDisplayName(),
smsMsg.getDate(), false));
else if (Integer.parseInt(smsMsg.getType()) == 2)
oneDisc.add(0,
new OneComment(false, smsMsg.getMsg(),
"Me", smsMsg.getDate(), false));
}
}
oneChat.put(con, oneDisc);
// add at pos 0
recentChats.add(0, oneChat);
}
}
return recentChats;
}
public String[] getContactByNumber(final String number) {
String[] data = new String[2];
try {
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
Cursor cur = _context.getContentResolver().query(uri,
new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },
null, null, null);
if (cur.moveToFirst()) {
int nameIdx = cur.getColumnIndex(PhoneLookup.DISPLAY_NAME);
data[0] = cur.getString(nameIdx);
String contactId = cur.getString(cur
.getColumnIndex(PhoneLookup._ID));
Uri photoUri = getContactPhotoUri(Long.parseLong(contactId));
if (photoUri != null)
data[1] = photoUri.toString();
else
data[1] = null;
cur.close();
return data;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Uri getContactPhotoUri(long contactId) {
Uri photoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
contactId);
photoUri = Uri.withAppendedPath(photoUri,
Contacts.Photo.CONTENT_DIRECTORY);
return photoUri;
}
}
SmsMsg.java POJO:
public class SmsMsg {
private String address = null;
private String displayName = null;
private String threadId = null;
private String date = null;
private String msg = null;
private String type = null;
Uri photoUri = null;
public Uri getPhotoUri() {
return photoUri;
}
public void setPhotoUri(Uri photoUri) {
this.photoUri = photoUri;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getThreadId() {
return threadId;
}
public void setThreadId(String threadId) {
this.threadId = threadId;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
最佳答案
POJO 存储不是一个很好的方法,因为它们需要一直保留在内存中,否则重新加载会导致痛苦和缓慢。
在这种情况下,您应该创建一个更新 sqlite 数据库的服务,并将其显示为 ContentProvider . sqlite 数据库应仅包含 Android 未提供的结构,即您的 Contact/Threads 层次结构,以及您可能在列表中显示的任何数据,例如最新消息的文本。
This thread讨论了如何检测新的 SMS 消息到达/发送,无论是从您的应用程序还是其他应用程序,这可能是您真正想要的,而不是简单地检测用户从您自己的应用程序发布消息。 Service应该执行这个任务,UI Activity只需要观察ContentProvider。
旁白:我想知道用户如何向他们尚未向其发送消息的联系人发送消息,因为您的列表仅包含他们已向其发送消息的联系人。
关于Android 从收件箱中获取短信,优化了读取所有消息并将它们分组的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14545661/
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit