SmsListActivity.java
public class SmsListActivity extends Activity {
private ListView listview;
private List<Sms> infos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms_list);
Uri uri = Uri.parse(AllFinalInfo.SMS_URI_ALL);
SmsContent sc=new SmsContent(this, uri);
infos = sc.getSmsInfo();
listview = (ListView) this.findViewById(R.id.ListView_Sms);
listview.setAdapter(new SmsListAdapter(this));
}
class SmsListAdapter extends BaseAdapter {
private LayoutInflater layoutinflater;
private View myview;
public SmsListAdapter(Context c) {
layoutinflater = LayoutInflater.from(c);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return infos.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// if (convertView == null) {
myview = layoutinflater.inflate(R.layout.smsitem, null);
// }
// TextView id = (TextView) myview.findViewById(R.id.TextView_SmsID);
TextView body = (TextView) myview.findViewById(R.id.TextView_SmsBody);
TextView name = (TextView) myview .findViewById(R.id.TextView_SmsName);
TextView number = (TextView) myview.findViewById(R.id.TextView_SmsNumber);
TextView date = (TextView) myview .findViewById(R.id.TextView_SmsDate);
TextView type = (TextView) myview.findViewById(R.id.TextView_SmsType);
// id.setText(infos.get(position).getId()+" ID");
body.setText(infos.get(position).getContent()+" 内容");
name.setText(infos.get(position).getName()+" 名字");
number.setText(infos.get(position).getNumber()+" 号码");
date.setText(infos.get(position).getDate()+" 日期");
type.setText(infos.get(position).getType()+" 发送或接受");
return myview;
}
}
}
Sms.java
public class Sms {
private String id;
private String name;
private String date;
private String number;
private String type;
private String content;
public Sms() {
super();
this.id = id;
this.name = name;
this.date = date;
this.number = number;
this.type = type;
this.content = content;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public class AllFinalInfo {
/**
* 所有的短信
*/
public static final String SMS_URI_ALL = "content://sms/";
/**
* 收件箱短信
*/
public static final String SMS_URI_INBOX = "content://sms/inbox";
/**
* 发件箱短信
*/
public static final String SMS_URI_SEND = "content://sms/sent";
/**
* 草稿箱短信
*/
public static final String SMS_URI_DRAFT = "content://sms/draft";
}
public static String getContactNameByAddr(Context context,
String phoneNumber) {
Uri personUri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cur = context.getContentResolver().query(personUri,
new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
if (cur.moveToFirst()) {
int nameIdx = cur.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String name = cur.getString(nameIdx);
cur.close();
return name;
}
return phoneNumber;
}
public static class SmsContent {
private Activity activity;
private Uri uri;
List<Sms> infos;
public SmsContent(Activity activity, Uri uri) {
infos = new ArrayList<Sms>();
this.activity = activity;
this.uri = uri;
}
@SuppressLint("SimpleDateFormat")
public List<Sms> getSmsInfo() {
/**
* String[] projection = new String[] { "_id", "address", "person",
"body", "date", "type" };
*/
String[] projection = new String[] {"address", "person",
"body", "date", "type" };
@SuppressWarnings("deprecation")
Cursor cusor = activity.managedQuery(uri, projection, null, null,
"date desc");
// int idColumn = cusor.getColumnIndex("_id");
int nameColumn = cusor.getColumnIndex("person");
int phoneNumberColumn = cusor.getColumnIndex("address");
int smsbodyColumn = cusor.getColumnIndex("body");
int dateColumn = cusor.getColumnIndex("date");
int typeColumn = cusor.getColumnIndex("type");
if (cusor != null) {
while (cusor.moveToNext()) {
Sms smsinfo = new Sms();
// smsinfo.setId(cusor.getString(idColumn));
String phoneNumber = cusor.getString(phoneNumberColumn);
String name=getContactNameByAddr(activity, phoneNumber);
smsinfo.setName(name);
// String phoneNumber = cusor.getString(phoneNumberColumn);
Date d = new Date(Long.parseLong(cusor.getString(dateColumn)));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd " + "\n" + "hh:mm:ss");
String date = dateFormat.format(d);
smsinfo.setDate(date);
smsinfo.setNumber(cusor.getString(phoneNumberColumn));
smsinfo.setContent(cusor.getString(smsbodyColumn));
smsinfo.setType(cusor.getString(typeColumn));
infos.add(smsinfo);
}
cusor.close();
}
return infos;
}
}
}
主布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ListView_Sms"/>
</LinearLayout>
List布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TextView_SmsID"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/TextView_SmsName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/TextView_SmsBody"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/TextView_SmsNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/TextView_SmsDate"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/TextView_SmsType"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
获取权限
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />