Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (1) MainActivity
- public class MainActivity extends Activity {
- private ListView listView = null;
- private List<SmsInfo> infos = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Uri uri = Uri.parse(AllFinalInfo.SMS_URI_INBOX);
- SmsContent smsContent = new SmsContent(MainActivity.this, uri);
- infos = smsContent.getSmsInfo();
- listView = (ListView) this.findViewById(R.id.sms_list);
- listView.setAdapter(new SmsListAdapter(getApplicationContext(), infos));
- }
- class SmsListAdapter extends BaseAdapter {
- private LayoutInflater layoutInflater = null;
- private List<SmsInfo> infos = null;
- public SmsListAdapter(Context context, List<SmsInfo> infos) {
- this.layoutInflater = LayoutInflater.from(context);
- this.infos = infos;
- }
- @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) {
- // TODO Auto-generated method stub
- if (convertView == null) {
- convertView = layoutInflater.inflate(R.layout.smsitem, null);
- }
- ((TextView) convertView.findViewById(R.id.sms_body)).setText(infos
- .get(position).getSmsbody());
- ((TextView) convertView.findViewById(R.id.sms_name)).setText(infos
- .get(position).getName());
- ((TextView) convertView.findViewById(R.id.sms_phonenumber))
- .setText(infos.get(position).getPhoneNumber());
- ((TextView) convertView.findViewById(R.id.sms_date)).setText(infos.get(position).getDate());
- return convertView;
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
- (2) SmsContent
- public class SmsContent {
- private Activity activity;
- private Uri uri;
- List<SmsInfo> infos;
- private String TAG = "SmsContent";
- public SmsContent(Activity activity, Uri uri) {
- infos = new ArrayList<SmsInfo>();
- this.activity = activity;
- this.uri = uri;
- }
- public List<SmsInfo> getSmsInfo() {
- String[] projection = new String[] { "_id", "address", "person",
- "body", "date", "type" };
- // @SuppressWarnings("deprecation")
- // Cursor cursor = activity.managedQuery(uri, projection, null, null,
- // "date desc");
- ContentResolver cr = activity.getContentResolver();
- Cursor cursor = cr.query(uri, projection, null, null, "date desc");
- int nameColumn = cursor.getColumnIndex("person");
- int phoneNumberColumn = cursor.getColumnIndex("address");
- int smsbodyColumn = cursor.getColumnIndex("body");
- int dateColumn = cursor.getColumnIndex("date");
- int typeColumn = cursor.getColumnIndex("type");
- if (cursor != null) {
- int i = 0;
- while (cursor.moveToNext() && i++ < 20) {
- SmsInfo smsInfo = new SmsInfo();
- smsInfo.setName(cursor.getString(nameColumn));
- smsInfo.setDate(dateFromLongToString(cursor.getString(dateColumn)));
- smsInfo.setPhoneNumber(cursor.getString(phoneNumberColumn));
- smsInfo.setSmsbody(cursor.getString(smsbodyColumn));
- smsInfo.setType(cursor.getString(typeColumn));
- String personName = getPeople2(smsInfo.getPhoneNumber());
- smsInfo.setName(null == personName ? smsInfo.getPhoneNumber()
- : personName);
- infos.add(smsInfo);
- }
- cursor.close();
- }
- return infos;
- }
- public String getPeople(String phoneNumber) {
- String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
- ContactsContract.CommonDataKinds.Phone.NUMBER };
- Log.d(TAG, "getPeople ---------");
- Cursor cursor = activity.getContentResolver().query(
- ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
- projection, // Which columns to return.
- ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
- + phoneNumber + "'", // WHERE clause.
- null, // WHERE clause value substitution
- null); // Sort order.
- if (cursor == null) {
- Log.d(TAG, "getPeople null");
- return null;
- }
- Log.d(TAG, "getPeople cursor.getCount() = " + cursor.getCount());
- for (int i = 0; i < cursor.getCount(); i++) {
- cursor.moveToPosition(i);
- int nameFieldColumnIndex = cursor
- .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
- String name = cursor.getString(nameFieldColumnIndex);
- Log.i("Contacts", "" + name + " .... " + nameFieldColumnIndex);
- return name;
- }
- return null;
- }
- public String getPeople2(String phoneNumber) {
- Cursor cursor = activity.getContentResolver().query(
- Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
- phoneNumber),
- new String[] { PhoneLookup._ID, PhoneLookup.NUMBER,
- PhoneLookup.DISPLAY_NAME, PhoneLookup.TYPE,
- PhoneLookup.LABEL }, null, null, null);
- if (cursor.getCount() == 0) {
- return null;
- } else if (cursor.getCount() > 0) {
- cursor.moveToFirst();
- return cursor.getString(2);
- }
- return null;
- }
- public String dateFromLongToString(String dateLong) {
- SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- java.util.Date dt = new Date(Long.parseLong(dateLong));
- return sdf.format(dt);
- }
- }
- (3)SmsInfo
- public class SmsInfo {
- private String smsbody;
- private String phoneNumber;
- private String date;
- private String name;
- private String type;
- public String getSmsbody() {
- return smsbody;
- }
- public void setSmsbody(String smsbody) {
- this.smsbody = smsbody;
- }
- public String getPhoneNumber() {
- return phoneNumber;
- }
- public void setPhoneNumber(String phoneNumber) {
- this.phoneNumber = phoneNumber;
- }
- public String getDate() {
- return date;
- }
- public void setDate(String date) {
- this.date = date;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- }
- (4) AllFinalInfo
- 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";
- }
Add Comment
Please, Sign In to add comment