Adi099

sms

Oct 15th, 2016
1,070
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. (1) MainActivity
  2.  
  3. public class MainActivity extends Activity {
  4.  
  5. private ListView listView = null;
  6. private List<SmsInfo> infos = null;
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12.  
  13. Uri uri = Uri.parse(AllFinalInfo.SMS_URI_INBOX);
  14. SmsContent smsContent = new SmsContent(MainActivity.this, uri);
  15. infos = smsContent.getSmsInfo();
  16. listView = (ListView) this.findViewById(R.id.sms_list);
  17. listView.setAdapter(new SmsListAdapter(getApplicationContext(), infos));
  18. }
  19.  
  20. class SmsListAdapter extends BaseAdapter {
  21.  
  22. private LayoutInflater layoutInflater = null;
  23. private List<SmsInfo> infos = null;
  24.  
  25. public SmsListAdapter(Context context, List<SmsInfo> infos) {
  26. this.layoutInflater = LayoutInflater.from(context);
  27. this.infos = infos;
  28. }
  29.  
  30. @Override
  31. public int getCount() {
  32. // TODO Auto-generated method stub
  33. return infos.size();
  34. }
  35.  
  36. @Override
  37. public Object getItem(int position) {
  38. // TODO Auto-generated method stub
  39. return null;
  40. }
  41.  
  42. @Override
  43. public long getItemId(int position) {
  44. // TODO Auto-generated method stub
  45. return 0;
  46. }
  47.  
  48. @Override
  49. public View getView(int position, View convertView, ViewGroup parent) {
  50. // TODO Auto-generated method stub
  51. if (convertView == null) {
  52. convertView = layoutInflater.inflate(R.layout.smsitem, null);
  53. }
  54. ((TextView) convertView.findViewById(R.id.sms_body)).setText(infos
  55. .get(position).getSmsbody());
  56. ((TextView) convertView.findViewById(R.id.sms_name)).setText(infos
  57. .get(position).getName());
  58. ((TextView) convertView.findViewById(R.id.sms_phonenumber))
  59. .setText(infos.get(position).getPhoneNumber());
  60. ((TextView) convertView.findViewById(R.id.sms_date)).setText(infos.get(position).getDate());
  61. return convertView;
  62. }
  63.  
  64. }
  65.  
  66. @Override
  67. public boolean onCreateOptionsMenu(Menu menu) {
  68. // Inflate the menu; this adds items to the action bar if it is present.
  69. getMenuInflater().inflate(R.menu.main, menu);
  70. return true;
  71. }
  72.  
  73. }
  74.  
  75. (2) SmsContent
  76.  
  77. public class SmsContent {
  78.  
  79. private Activity activity;
  80. private Uri uri;
  81. List<SmsInfo> infos;
  82. private String TAG = "SmsContent";
  83.  
  84. public SmsContent(Activity activity, Uri uri) {
  85. infos = new ArrayList<SmsInfo>();
  86. this.activity = activity;
  87. this.uri = uri;
  88. }
  89.  
  90. public List<SmsInfo> getSmsInfo() {
  91. String[] projection = new String[] { "_id", "address", "person",
  92. "body", "date", "type" };
  93.  
  94. // @SuppressWarnings("deprecation")
  95. // Cursor cursor = activity.managedQuery(uri, projection, null, null,
  96. // "date desc");
  97.  
  98. ContentResolver cr = activity.getContentResolver();
  99. Cursor cursor = cr.query(uri, projection, null, null, "date desc");
  100.  
  101. int nameColumn = cursor.getColumnIndex("person");
  102. int phoneNumberColumn = cursor.getColumnIndex("address");
  103. int smsbodyColumn = cursor.getColumnIndex("body");
  104. int dateColumn = cursor.getColumnIndex("date");
  105. int typeColumn = cursor.getColumnIndex("type");
  106. if (cursor != null) {
  107. int i = 0;
  108. while (cursor.moveToNext() && i++ < 20) {
  109. SmsInfo smsInfo = new SmsInfo();
  110. smsInfo.setName(cursor.getString(nameColumn));
  111. smsInfo.setDate(dateFromLongToString(cursor.getString(dateColumn)));
  112. smsInfo.setPhoneNumber(cursor.getString(phoneNumberColumn));
  113. smsInfo.setSmsbody(cursor.getString(smsbodyColumn));
  114. smsInfo.setType(cursor.getString(typeColumn));
  115. String personName = getPeople2(smsInfo.getPhoneNumber());
  116. smsInfo.setName(null == personName ? smsInfo.getPhoneNumber()
  117. : personName);
  118. infos.add(smsInfo);
  119. }
  120. cursor.close();
  121. }
  122. return infos;
  123. }
  124.  
  125.  
  126. public String getPeople(String phoneNumber) {
  127. String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
  128. ContactsContract.CommonDataKinds.Phone.NUMBER };
  129.  
  130. Log.d(TAG, "getPeople ---------");
  131.  
  132.  
  133. Cursor cursor = activity.getContentResolver().query(
  134. ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  135. projection, // Which columns to return.
  136. ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
  137. + phoneNumber + "'", // WHERE clause.
  138. null, // WHERE clause value substitution
  139. null); // Sort order.
  140.  
  141. if (cursor == null) {
  142. Log.d(TAG, "getPeople null");
  143. return null;
  144. }
  145. Log.d(TAG, "getPeople cursor.getCount() = " + cursor.getCount());
  146. for (int i = 0; i < cursor.getCount(); i++) {
  147. cursor.moveToPosition(i);
  148.  
  149.  
  150. int nameFieldColumnIndex = cursor
  151. .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
  152. String name = cursor.getString(nameFieldColumnIndex);
  153. Log.i("Contacts", "" + name + " .... " + nameFieldColumnIndex);
  154. return name;
  155. }
  156. return null;
  157. }
  158.  
  159. public String getPeople2(String phoneNumber) {
  160. Cursor cursor = activity.getContentResolver().query(
  161. Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
  162. phoneNumber),
  163. new String[] { PhoneLookup._ID, PhoneLookup.NUMBER,
  164. PhoneLookup.DISPLAY_NAME, PhoneLookup.TYPE,
  165. PhoneLookup.LABEL }, null, null, null);
  166.  
  167. if (cursor.getCount() == 0) {
  168.  
  169. return null;
  170. } else if (cursor.getCount() > 0) {
  171.  
  172. cursor.moveToFirst();
  173. return cursor.getString(2);
  174. }
  175. return null;
  176. }
  177.  
  178. public String dateFromLongToString(String dateLong) {
  179. SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  180.  
  181. java.util.Date dt = new Date(Long.parseLong(dateLong));
  182. return sdf.format(dt);
  183. }
  184.  
  185. }
  186.  
  187. (3)SmsInfo
  188.  
  189. public class SmsInfo {
  190.  
  191. private String smsbody;
  192. private String phoneNumber;
  193. private String date;
  194. private String name;
  195. private String type;
  196.  
  197. public String getSmsbody() {
  198. return smsbody;
  199. }
  200.  
  201. public void setSmsbody(String smsbody) {
  202. this.smsbody = smsbody;
  203. }
  204.  
  205. public String getPhoneNumber() {
  206. return phoneNumber;
  207. }
  208.  
  209. public void setPhoneNumber(String phoneNumber) {
  210. this.phoneNumber = phoneNumber;
  211. }
  212.  
  213. public String getDate() {
  214. return date;
  215. }
  216.  
  217. public void setDate(String date) {
  218. this.date = date;
  219. }
  220.  
  221. public String getName() {
  222. return name;
  223. }
  224.  
  225. public void setName(String name) {
  226. this.name = name;
  227. }
  228.  
  229. public String getType() {
  230. return type;
  231. }
  232.  
  233. public void setType(String type) {
  234. this.type = type;
  235. }
  236.  
  237. }
  238. (4) AllFinalInfo
  239.  
  240. public class AllFinalInfo {
  241.  
  242. public static final String SMS_URI_ALL = "content://sms/";
  243. public static final String SMS_URI_INBOX = "content://sms/inbox/";
  244. public static final String SMS_URI_SEND = "content://sms/sent";
  245. public static final String SMS_URI_DRAFT = "content://sms/draft";
  246.  
  247. }
Add Comment
Please, Sign In to add comment