从收件箱中的Android获取短信,优化的方式来阅读所有邮件,并将它们组合 [英] Android get sms from inbox, optimized way to read all messages and group them
问题描述
你好我实现一个短信应用程序,现在我能够检索与像显示姓名,照片URI各自的联系方式的所有消息..和我在对项目点击带你到相应的讨论自定义列表中显示它们。在这里,我的问题是采取同步所有这些信息的时候,
- 我需要优化这个时候。
- 在每一次我发送新邮件的讨论来看,回到最近的聊天记录我只需要更新特定的项目,而不是整个列表。
下面是我的code:
ReadSMS.java:
公共类ReadSMS {
ArrayList的< HashMap的<联系,ArrayList的< OneComment>>> recentChats;
应用_context;
公共ReadSMS(应用程序上下文){
this._context =背景;
this.recentChats =((ChatApplication)_context).getChats();
}
公众的ArrayList< HashMap的<联系,ArrayList的< OneComment>>> getSMS(){
// 在里面
ArrayList的< SmsMsg> smsMsgs =新的ArrayList< SmsMsg>();
TreeSet中<整数GT; threadIds =新TreeSet的<整数GT;();
乌里mSmsinboxQueryUri = Uri.parse(内容:// SMS);
光标光标= _context.getContentResolver()查询(
mSmsinboxQueryUri,
新的String [] {_id,thread_id单,地址,日,身体,
型},NULL,NULL,NULL);
的String []列=新的String [] {地址,thread_id单,日期,
体型 };
如果(cursor.getCount()大于0){
而(cursor.moveToNext()){
SmsMsg smsMsg =新SmsMsg();
字符串的地址= null,则显示名= NULL,日期= NULL,味精= NULL,类型= NULL,threadId的= NULL;
乌里photoUri = NULL;
threadId的= cursor.getString(cursor.getColumnIndex(列[1]));
类型= cursor.getString(cursor.getColumnIndex(列[4]));
如果(的Integer.parseInt(型)== 1 ||的Integer.parseInt(型)== 2){
地址= cursor.getString(光标
.getColumnIndex(列[0]));
如果(address.length()大于0){
的String []联系数据= getContactByNumber(地址);
如果(联络人资料!= NULL){
显示名=联系数据[0];
如果(联络人资料[1]!= NULL)
photoUri = Uri.parse(联系数据[1]);
}
} 其他
地址= NULL;
日期= cursor.getString(cursor.getColumnIndex(列[2]));
味精= cursor.getString(cursor.getColumnIndex(列[3]));
smsMsg.setDisplayName(显示名);
smsMsg.setThreadId(threadId的);
smsMsg.setAddress(地址);
smsMsg.setPhotoUri(photoUri);
smsMsg.setDate(日期);
smsMsg.setMsg(MSG);
smsMsg.setType(类型);
// Log.e(短信收件箱,\ñ\ n名称:+显示名
// +\ nTHREAD_ID:+ threadId的+\ n已接收:
// +地址+\ nPHOTO_URI:+ photoUri +\ n时间:
// +日期+\ n消息:+ MSG +\ n-型:+型);
smsMsgs.add(smsMsg);
//添加threadId的到树
threadIds.add(的Integer.parseInt(threadId的));
}
}
对于(INT threadId的:threadIds){
HashMap的<联系,ArrayList的< OneComment>> oneChat =新的HashMap<联系,ArrayList的< OneComment>>();
联系CON =新联系();
ArrayList的< OneComment> oneDisc =新的ArrayList< OneComment>();
对于(SmsMsg smsMsg:smsMsgs){
如果(的Integer.parseInt(smsMsg.getThreadId())== threadId的){
con.setContactName(smsMsg.getDisplayName());
con.setContactNumber(smsMsg.getAddress());
con.setContactPhotoUri(smsMsg.getPhotoUri());
如果(的Integer.parseInt(smsMsg.getType())== 1)
oneDisc.add(0,新OneComment(真,
smsMsg.getMsg(),smsMsg.getDisplayName(),
smsMsg.getDate(),FALSE));
否则如果(的Integer.parseInt(smsMsg.getType())== 2)
oneDisc.add(0,
新OneComment(假,smsMsg.getMsg()
我,smsMsg.getDate(),FALSE));
}
}
oneChat.put(CON,oneDisc);
//增加在POS 0
recentChats.add(0,oneChat);
}
}
返回recentChats;
}
公众的String [] getContactByNumber(最终串号){
的String []数据=新的String [2];
尝试 {
开放的我们的uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.en code(数));
光标CUR = _context.getContentResolver()查询(URI,
新的String [] {PhoneLookup.DISPLAY_NAME,PhoneLookup._ID},
NULL,NULL,NULL);
如果(cur.moveToFirst()){
INT nameIdx = cur.getColumnIndex(PhoneLookup.DISPLAY_NAME);
数据[0] = cur.getString(nameIdx);
字符串的ContactID = cur.getString(CUR
.getColumnIndex(PhoneLookup._ID));
乌里photoUri = getContactPhotoUri(的Long.parseLong(的ContactID));
如果(photoUri!= NULL)
数据[1] = photoUri.toString();
其他
数据[1] =无效;
cur.close();
返回的数据;
}
}赶上(例外五){
e.printStackTrace();
}
返回null;
}
公众开放的getContactPhotoUri(长的ContactID){
乌里photoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
的ContactID);
photoUri = Uri.withAppendedPath(photoUri,
Contacts.Photo.CONTENT_DIRECTORY);
返回photoUri;
}
}
SmsMsg.java POJO:
公共类SmsMsg {
私人字符串地址= NULL;
私人字符串显示名= NULL;
私人字符串threadId的= NULL;
私人字符串日期= NULL;
私人弦乐味精= NULL;
私有String类型= NULL;
乌里photoUri = NULL;
公共乌里getPhotoUri(){
返回photoUri;
}
公共无效setPhotoUri(URI photoUri){
this.photoUri = photoUri;
}
公共字符串getDisplayName(){
回报显示名;
}
公共无效setDisplayName(字符串显示名){
this.displayName =显示名;
}
公共字符串的getAddress(){
退货地址;
}
公共无效setAddress(字符串地址){
this.address =地址;
}
公共字符串getThreadId(){
返回threadId的;
}
公共无效setThreadId(字符串threadId的){
this.threadId = threadId的;
}
公共字符串GETDATE(){
返回日期;
}
公共无效的setDate(字符串日期){
this.date =日期;
}
公众的getMsg字符串(){
返回味精;
}
公共无效setMsg(弦乐味精){
this.msg =味精;
}
公共字符串的getType(){
返回类型;
}
公共无效的setType(字符串类型){
this.type =类型;
}
}
POJO存储是不是一个伟大的方式,因为这些被要求留在内存中的所有时间,否则将重新加载引起疼痛和缓慢。
在这种情况下,你应该创建一个服务,更新SQLite数据库,和present它作为一个<一个href="http://developer.android.com/guide/topics/providers/content-providers.html">ContentProvider. sqlite的分贝应该只包含不提供的Android的,即你的联系方式/线程层次结构,和任何数据,你可能会显示在列表中,比如最新的消息的文本。
此线程有如何检测新的SMS消息到达的讨论/发,无论是从您的应用程序或其他,这可能是你真正想要什么,而不是简单地检测到用户从发布自己的应用程序的消息。该服务应执行此任务,用户界面活动只需要观察的ContentProvider。
另外:我想知道用户将消息发送给联系人向他们尚未发送的消息,因为你的列表仅包含他们已发送的邮件,以谁的联系人
Hi am implementing a SMS App, now am able to retrieve all messages with their respective contact info like display name, photo uri.. and am displaying them in a custom list where on item click takes you to respective discussion. Here my issues is the time taking to sync all these messages,
- I need to optimize this time.
- Each time i send a new message in the discussion view and go back to recent chats i need to only update the particular item, not the whole list.
Here's my code:
ReadSMS.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 storage is not a great approach since those are required to stay in memory all the time, or else be reloaded causing pain and slowness.
In this case, you should create a Service that updates a sqlite database, and present it as a ContentProvider. The sqlite db should only contain the structure that is not provided by Android, i.e. your Contact/Threads hierarchy, and any data you might be displaying in your list, such as the text of the most recent message.
This thread has a discussion of how to detect new SMS Message arrival/sends, whether from your app or another, which is probably what you really want rather than simply detecting that the user posted a message from your own app. The Service should perform this task, the UI Activity only needs to observe the ContentProvider.
Aside: I wonder how the user will send a message to a contact to whom they have not yet sent a message, since your list only contains contacts who they have sent messages to.
这篇关于从收件箱中的Android获取短信,优化的方式来阅读所有邮件,并将它们组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!