短信数据库的存储路径:/data/data/com.android.providers.telephony/databases/mmssms.db
//插入短信数据到短信数据库中
public boolean DoImportMessageConversation(ArrayList<Conversation> conversations)
{
boolean isInserted =false;
//支持android4.4一下版本
int type;
if(conversations.size()==0)
{
return isInserted;
}
for(int i =0;i<conversations.size();i++)
{
Conversation ii =conversations.get(i);
g_chats_size =ii.chats.size();
for(int j =0;j<ii.chats.size();j++)
{
ChatMessage ms =ii.chats.get(j);
isInserted =false;
//1为收 2为发
type =ms.type;
Uri uri =null;
if(type ==SMS_TYPE_SENT)
{
uri =Uri.parse(SMS_URI_INBOX);
}
else
{
uri =Uri.parse(SMS_URI_SEND);
}
Long timeDate =transferStringDateToLong(ms.dateFormatted,ms.date);
ContentResolver tempResolver =getContentResolver();
ContentValues values =new ContentValues();
values.put("read", ms.read);
values.put("address", ii.address);
//1为收 2为发
values.put("type", String.valueOf(type));
values.put("date", timeDate);
values.put("body", ms.body);
uri =tempResolver.insert(uri, values);
if(uri!=null)
{
isInserted =true;
ContentResolver resolver = getContentResolver();
Cursor cursor2 = resolver.query(uri, new String[]{"_id","thread_id","address","person","body","date","type","read"},null, null,null);
if(cursor2!=null&&cursor2.getCount()>0)
{
int _id;
int thread_id;
String address;
String name;
String date;
String body;
cursor2.moveToNext();
_id = cursor2.getInt(0);
thread_id = cursor2.getInt(1);
address = cursor2.getString(2);
name = cursor2.getString(3);
body = cursor2.getString(4);
date = cursor2.getString(5);
g_chats_size--;
if(g_chats_size ==0)
{
//封装一个会话信息
//最后一条短信的日期和内容
Conversation conversation =new Conversation();
conversation.id =String.valueOf(thread_id);
conversation.address=address;
conversation.date =date;
conversation.dateFormatted =date;
conversation.snippet =body;
String result = "id:" + thread_id + " snippet:" + body +"date:"+date;
Log.i("==========",result);
//把会话对象进行封装传到C++底层
}
}
cursor2.close();
}
}
}
return isInserted;
}
其中g_chats_size是一个全局变量,用于记录插入的同一个联系人的多条短信记录