[android]_[初级][插入短信数据到短信数据库]

短信数据库的存储路径:/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是一个全局变量,用于记录插入的同一个联系人的多条短信记录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值