package com.xx.readsms;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public
class ReadSmsActivity
extends Activity {
/**
Called when the activity is first created.
*/
private Context mContext;
private TextView tv;
private
static List<ContactInfo> list=
new ArrayList<ContactInfo>();
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)
this.findViewById(R.id.tv_test);
getSMSInfo();
tv.setOnClickListener(
new OnClickListener() {
@Override
public
void onClick(View v) {
//
TODO Auto-generated method stub
list.clear();
System.out.println("=================1");
getContacts_LastMessage();
System.out.println("=================2");
StringBuilder sb=
new StringBuilder();
if(list!=
null){
System.out.println("=================3");
for (
int i = 0; i < list.size(); i++) {
System.out.println("=================4:"+i);
ContactInfo tmp=
new ContactInfo();
tmp=(ContactInfo)list.get(i);
sb.append("["+tmp.getContact()+",");
sb.append(tmp.getLast_mms()+",");
sb.append(tmp.getDate_mms()+",");
sb.append(getCanonicalAddresses(tmp.getRecipient())+",");
//
去canonical_addresses表获取接收者号码
sb.append(tmp.getFinal_count()+"]\n");
}
tv.setText("");
tv.setText(sb.toString());
}
else{
System.out.println("list is null");
}
}
});
}
public String getCanonicalAddresses(String recipient_ids){
String[] ids=recipient_ids.split(" ");
System.out.println("recipient_ids:"+recipient_ids);
String addressResult="";
for (
int i = 0; i < ids.length; i++) {
Cursor cur=ReadSmsActivity.
this.getContentResolver().query(Uri.parse("content://mms/"),
new String[]{" * from canonical_addresses where _id="+ids[i].toString()+" --"},
null,
null,
null);
if(cur.getCount()>0){
while (cur.moveToNext()){
String _id=cur.getString(cur.getColumnIndex("_id"));
String address=cur.getString(cur.getColumnIndex("address"));
addressResult+="{"+_id+","+address+"}";
}
}
}
return addressResult;
}
/**
* 从mms数据库中检索threads表
*/
public
void getContacts_LastMessage()
{
ContentResolver cr = ReadSmsActivity.
this.getContentResolver();
Cursor cursor = cr.query(Uri.parse("content://mms/"),
new String[]
{ "* from threads--" },
null,
null,
null);
while (cursor.moveToNext())
{
int thread_id = cursor.getColumnIndex("_id");
int date = cursor.getColumnIndex("date");
int message_count = cursor.getColumnIndex("message_count");
int snippet = cursor.getColumnIndex("snippet");
int recipient=cursor.getColumnIndex("recipient_ids");
//
格式化短信日期显示
SimpleDateFormat sfd =
new SimpleDateFormat("MM-dd hh:mm:ss");
Date date_format =
new Date(Long.parseLong(cursor.getString(date)));
String time = sfd.format(date_format);
//
获得短信的各项内容
String info[]=getPhoneNum(cursor.getString(thread_id));
String last_mms=cursor.getString(snippet);
String date_mms=time;
String count_mms=cursor.getString(message_count);
String recipient_ids=cursor.getString(recipient);
//
判断是否联系人
String contact = getContactNameFromPhoneNum(
this.getApplicationContext(), info[0]);
//
获得最后的未读短信与已读短信
String final_count="("+info[1]+"/"+count_mms+")";
//
添加到list
if (contact.equals(""))
{
getData(
info[0],
last_mms,
final_count,
date_mms,
recipient_ids
);
}
else
{
getData(
contact,
last_mms,
final_count,
date_mms,
recipient_ids
);
}
}
}
/**
* 获取联系人信息
*
@param
context
*
@param
contactInfo
*
@return
*/
public String getContactNameFromPhoneNum(Context context,String contactInfo){
Long curPersonID;
String contactNames = "";
Uri uri = Uri.parse("content://com.android.contacts/phone_lookup/" + contactInfo);
Cursor cursor = getContentResolver().query(uri,
null,
null,
null,
null);
if(cursor.moveToFirst()){
String contactName = cursor.getString(cursor.getColumnIndex("display_name"));
curPersonID = cursor.getLong(cursor.getColumnIndex("_id"));
cursor.close();
if(contactNames=="")
contactNames = contactName;
else
contactNames = contactNames+","+contactName;
}
return contactNames;
}
/**
* 添加到列表
*
@param
contact
*
@param
last_mms
*
@param
final_count
*
@param
date_mms
*/
public
void getData(String contact,String last_mms,String final_count,String date_mms,String recipient_ids){
ContactInfo c=
new ContactInfo();
if(contact!=
null) c.setContact(contact);
if(date_mms!=
null) c.setDate_mms(date_mms);
if(final_count!=
null) c.setFinal_count(final_count);
if(last_mms!=
null) c.setLast_mms(last_mms);
if(recipient_ids!=
null) c.setRecipient(recipient_ids);
//
System.out.println(contact+" "+date_mms);
list.add(c);
}
/**
*
*
@param
根据thread_id 检索sms库, 获得对应的号码
*
@return
*/
public String[] getPhoneNum(String thread_id)
{
String PhoneNum="";
int noread_mms=0;
String[] info={"",""};
String[] projection =
new String[]
{ "thread_id", "address", "person", "body", "date", "type","read" };
Uri uri = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query
(
uri,
projection,
"thread_id=?",
new String[] { thread_id } ,
null
);
while (cursor.moveToNext())
{
int phoneNumber = cursor.getColumnIndex("address");
int isread =cursor.getColumnIndex("read");
if (cursor.getString(isread).equals("0"))
{
noread_mms++;
}
PhoneNum=cursor.getString(phoneNumber);
}
info[0]=PhoneNum;
info[1]=Integer.toString(noread_mms);
return info;
}
}