zoukankan      html  css  js  c++  java
  • Android通讯录管理(获取联系人、通话记录、短信消息)(三)

    原文:
    http://blog.csdn.net/wwj_748/article/details/19984941
     
    这是通讯录管理的最后一篇,前面两篇已经把获取联系人和通话记录解决了,短息消息就相对来说要稍微复杂那么一点。我们先来看看效果图:
    源码下载:http://download.csdn.net/detail/wwj_748/6962865
     

       
     
     
     
     
     
    首先显示短信列表,每个列表会显示发送人号码,日期还有短信的内容概要,点击列表之后进入穿查看会话的短信消息,这里为了简单起见就没有把发送消息的功能加进去了。
     
    布局界面:
    /Contact_Demo/res/layout/sms_list_view.xml
    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="match_parent"  
    5.     android:background="#000000"  
    6.     android:orientation="vertical" >  
    7.   
    8.     <ListView  
    9.         android:id="@+id/sms_list"  
    10.         android:layout_width="fill_parent"  
    11.         android:layout_height="wrap_content"  
    12.         android:layout_gravity="center"  
    13.         android:cacheColorHint="#00000000"  
    14.         android:choiceMode="singleChoice"  
    15.         android:fastScrollEnabled="true"  
    16.         android:soundEffectsEnabled="true" />  
    17.   
    18. </LinearLayout>  

    /Contact_Demo/res/layout/sms_list_item.xml
    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="wrap_content"  
    5.     android:gravity="center_vertical"  
    6.     android:paddingTop="5dip" >  
    7.   
    8.     <QuickContactBadge  
    9.         android:id="@+id/qcb"  
    10.         android:layout_width="75dip"  
    11.         android:layout_height="75dip"  
    12.         android:layout_marginBottom="3dip"  
    13.         android:src="@drawable/touxiang" />  
    14.   
    15.     <LinearLayout  
    16.         android:id="@+id/ll"  
    17.         android:layout_width="wrap_content"  
    18.         android:layout_height="wrap_content"  
    19.         android:layout_marginLeft="3dip"  
    20.         android:layout_toRightOf="@+id/qcb"  
    21.         android:gravity="center_vertical"  
    22.         android:orientation="horizontal" >  
    23.   
    24.         <TextView  
    25.             android:id="@+id/name"  
    26.             android:layout_width="wrap_content"  
    27.             android:layout_height="wrap_content"  
    28.             android:singleLine="true"  
    29.             android:textAppearance="?android:attr/textAppearanceMedium"  
    30.             android:textColor="#ffffff" />  
    31.   
    32.         <TextView  
    33.             android:id="@+id/count"  
    34.             android:layout_width="wrap_content"  
    35.             android:layout_height="wrap_content"  
    36.             android:layout_marginLeft="5dip"  
    37.             android:singleLine="true"  
    38.             android:textAppearance="?android:attr/textAppearanceSmall"  
    39.             android:textColor="#ffffff" />  
    40.     </LinearLayout>  
    41.   
    42.     <TextView  
    43.         android:id="@+id/date"  
    44.         android:layout_width="wrap_content"  
    45.         android:layout_height="wrap_content"  
    46.         android:layout_below="@+id/ll"  
    47.         android:layout_marginLeft="3dip"  
    48.         android:layout_toRightOf="@+id/qcb"  
    49.         android:singleLine="true"  
    50.         android:textAppearance="?android:attr/textAppearanceSmall"  
    51.         android:textColor="#ffffff" />  
    52.   
    53.     <TextView  
    54.         android:id="@+id/content"  
    55.         android:layout_width="wrap_content"  
    56.         android:layout_height="wrap_content"  
    57.         android:layout_below="@+id/date"  
    58.         android:layout_gravity="bottom"  
    59.         android:layout_marginLeft="3dip"  
    60.         android:layout_toRightOf="@+id/qcb"  
    61.         android:singleLine="true"  
    62.         android:textAppearance="?android:attr/textAppearanceMedium"  
    63.         android:textColor="#5CACEE" />  
    64.   
    65. </RelativeLayout>  


    短息实体类:
     
    /Contact_Demo/src/com/suntek/contact/model/SMSBean.java
    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. package com.suntek.contact.model;  
    2.   
    3. public class SMSBean {  
    4.   
    5.     private String thread_id; // 线程id  
    6.     private String msg_count; // 消息个数  
    7.     private String msg_snippet; // 消息片段  
    8.     private String address; // 地址  
    9.     private Long date; // 日期  
    10.     private String read; // 已读  
    11.   
    12.     public SMSBean(String threadId, String msgCount, String msgSnippet) {  
    13.         thread_id = threadId;  
    14.         msg_count = msgCount;  
    15.         msg_snippet = msgSnippet;  
    16.     }  
    17.   
    18.     public SMSBean() {  
    19.     }  
    20.   
    21.     public String getAddress() {  
    22.         return address;  
    23.     }  
    24.   
    25.     public void setAddress(String address) {  
    26.         this.address = address;  
    27.     }  
    28.   
    29.     public Long getDate() {  
    30.         return date;  
    31.     }  
    32.   
    33.     public void setDate(Long date) {  
    34.         this.date = date;  
    35.     }  
    36.   
    37.     public String getRead() {  
    38.         return read;  
    39.     }  
    40.   
    41.     public void setRead(String read) {  
    42.         this.read = read;  
    43.     }  
    44.   
    45.     public String getThread_id() {  
    46.         return thread_id;  
    47.     }  
    48.   
    49.     public void setThread_id(String threadId) {  
    50.         thread_id = threadId;  
    51.     }  
    52.   
    53.     public String getMsg_count() {  
    54.         return msg_count;  
    55.     }  
    56.   
    57.     public void setMsg_count(String msgCount) {  
    58.         msg_count = msgCount;  
    59.     }  
    60.   
    61.     public String getMsg_snippet() {  
    62.         return msg_snippet;  
    63.     }  
    64.   
    65.     public void setMsg_snippet(String msgSnippet) {  
    66.         msg_snippet = msgSnippet;  
    67.     }  
    68. }  
     
    短信列表适配器:
    /Contact_Demo/src/com/suntek/contact/adapter/SMSAdpter.java
    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. package com.suntek.contact.adapter;  
    2.   
    3. import java.text.SimpleDateFormat;  
    4. import java.util.ArrayList;  
    5. import java.util.Date;  
    6. import java.util.List;  
    7.   
    8. import android.content.Context;  
    9. import android.view.LayoutInflater;  
    10. import android.view.View;  
    11. import android.view.ViewGroup;  
    12. import android.widget.BaseAdapter;  
    13. import android.widget.TextView;  
    14.   
    15. import com.suntek.contact.R;  
    16. import com.suntek.contact.model.SMSBean;  
    17.   
    18. /** 
    19.  * 短信适配器 
    20.  *  
    21.  * @author Administrator 
    22.  *  
    23.  */  
    24. public class SMSAdpter extends BaseAdapter {  
    25.     private LayoutInflater mInflater;  
    26.     private List<SMSBean> smsList;  
    27.     private Date date;  
    28.     private SimpleDateFormat sdf;  
    29.   
    30.     public SMSAdpter(Context context) {  
    31.         mInflater = LayoutInflater.from(context);  
    32.         this.smsList = new ArrayList<SMSBean>();  
    33.         this.date = new Date();  
    34.         this.sdf = new SimpleDateFormat("MM/dd HH:mm");  
    35.     }  
    36.   
    37.     public void assignment(List<SMSBean> smsList) {  
    38.         this.smsList = smsList;  
    39.     }  
    40.   
    41.     public void add(SMSBean bean) {  
    42.         smsList.add(bean);  
    43.     }  
    44.   
    45.     public void remove(int position) {  
    46.         smsList.remove(position);  
    47.     }  
    48.   
    49.     @Override  
    50.     public int getCount() {  
    51.         return smsList.size();  
    52.     }  
    53.   
    54.     @Override  
    55.     public Object getItem(int position) {  
    56.         return smsList.get(position);  
    57.     }  
    58.   
    59.     @Override  
    60.     public long getItemId(int position) {  
    61.         return position;  
    62.     }  
    63.   
    64.     @Override  
    65.     public View getView(int position, View convertView, ViewGroup parent) {  
    66.         ViewHolder holder = null;  
    67.         if (convertView == null) {  
    68.             convertView = mInflater.inflate(R.layout.sms_list_item, parent,  
    69.                     false);  
    70.             holder = new ViewHolder();  
    71.             holder.name = (TextView) convertView.findViewById(R.id.name);  
    72.             holder.count = (TextView) convertView.findViewById(R.id.count);  
    73.             holder.date = (TextView) convertView.findViewById(R.id.date);  
    74.             holder.content = (TextView) convertView.findViewById(R.id.content);  
    75.         } else {  
    76.             holder = (ViewHolder) convertView.getTag();  
    77.         }  
    78.   
    79.         holder.name.setText(smsList.get(position).getAddress());  
    80.         holder.count.setText("(" + smsList.get(position).getMsg_count() + ")");  
    81.   
    82.         this.date.setTime(smsList.get(position).getDate());  
    83.         holder.date.setText(this.sdf.format(date));  
    84.   
    85.         holder.content.setText(smsList.get(position).getMsg_snippet());  
    86.   
    87.         convertView.setTag(holder);  
    88.         return convertView;  
    89.     }  
    90.   
    91.     public final class ViewHolder {  
    92.         public TextView name;  
    93.         public TextView count;  
    94.         public TextView date;  
    95.         public TextView content;  
    96.     }  
    97. }  

    Activity界面
    /Contact_Demo/src/com/suntek/contact/SMSListActivity.java
    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. package com.suntek.contact;  
    2.   
    3. import java.util.HashMap;  
    4. import java.util.List;  
    5. import java.util.Map;  
    6.   
    7. import android.app.Activity;  
    8. import android.os.Bundle;  
    9. import android.view.View;  
    10. import android.widget.AdapterView;  
    11. import android.widget.AdapterView.OnItemClickListener;  
    12. import android.widget.ListView;  
    13.   
    14. import com.suntek.contact.adapter.SMSAdpter;  
    15. import com.suntek.contact.model.SMSBean;  
    16. import com.suntek.contact.util.BaseIntentUtil;  
    17. import com.suntek.contact.util.RexseeSMS;  
    18.   
    19. /** 
    20.  * 短信列表 
    21.  *  
    22.  * @author Administrator 
    23.  *  
    24.  */  
    25. public class SMSListActivity extends Activity {  
    26.   
    27.     private ListView smsListView;  
    28.     private SMSAdpter smsAdpter;  
    29.     private RexseeSMS rsms;  
    30.   
    31.     @Override  
    32.     protected void onCreate(Bundle savedInstanceState) {  
    33.         super.onCreate(savedInstanceState);  
    34.         setContentView(R.layout.sms_list_view);  
    35.         smsListView = (ListView) findViewById(R.id.sms_list);  
    36.         smsAdpter = new SMSAdpter(SMSListActivity.this);  
    37.         rsms = new RexseeSMS(SMSListActivity.this);  
    38.         List<SMSBean> list_mmt = rsms.getThreadsNum(rsms.getThreads(0));  
    39.         // 注入短信列表数据  
    40.         smsAdpter.assignment(list_mmt);  
    41.         // 填充数据  
    42.         smsListView.setAdapter(smsAdpter);  
    43.         // 短信列表项点击事件  
    44.         smsListView.setOnItemClickListener(new OnItemClickListener() {  
    45.   
    46.             @Override  
    47.             public void onItemClick(AdapterView<?> parent, View view,  
    48.                     int position, long id) {  
    49.                 Map<String, String> map = new HashMap<String, String>();  
    50.                 SMSBean sb = (SMSBean) smsAdpter.getItem(position);  
    51.                 map.put("phoneNumber", sb.getAddress());  
    52.                 map.put("threadId", sb.getThread_id());  
    53.                 BaseIntentUtil.intentSysDefault(SMSListActivity.this,  
    54.                         MessageBoxList.class, map);  
    55.             }  
    56.         });  
    57.     }  
    58. }  


    查询短信的工具类:
    /Contact_Demo/src/com/suntek/contact/util/RexseeSMS.java
    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. package com.suntek.contact.util;  
    2.   
    3. import java.util.ArrayList;  
    4. import java.util.List;  
    5.   
    6. import android.content.ContentResolver;  
    7. import android.content.Context;  
    8. import android.database.Cursor;  
    9. import android.net.Uri;  
    10.   
    11. import com.suntek.contact.model.SMSBean;  
    12.   
    13. public class RexseeSMS {  
    14.   
    15.     public static final String CONTENT_URI_SMS = "content://sms"; // 短信  
    16.     public static final String CONTENT_URI_SMS_INBOX = "content://sms/inbox";// 收件箱  
    17.     public static final String CONTENT_URI_SMS_SENT = "content://sms/sent"; // 发送  
    18.     public static final String CONTENT_URI_SMS_CONVERSATIONS = "content://sms/conversations";  
    19.   
    20.     public RexseeSMS(Context mContext) {  
    21.         this.mContext = mContext;  
    22.         // TODO Auto-generated constructor stub  
    23.     }  
    24.   
    25.     public static String[] SMS_COLUMNS = new String[] { "_id", // 0  
    26.             "thread_id", // 1  
    27.             "address", // 2  
    28.             "person", // 3  
    29.             "date", // 4  
    30.             "body", // 5  
    31.             "read", // 6; 0:not read 1:read; default is 0  
    32.             "type", // 7; 0:all 1:inBox 2:sent 3:draft 4:outBox 5:failed  
    33.                     // 6:queued  
    34.             "service_center" // 8  
    35.     };  
    36.     public static String[] THREAD_COLUMNS = new String[] { "thread_id",  
    37.             "msg_count", "snippet" };  
    38.   
    39.     private Context mContext;  
    40.   
    41.     public String getContentUris() {  
    42.         String rtn = "{";  
    43.         rtn += ""sms":"" + CONTENT_URI_SMS + """;  
    44.         rtn += ","inbox":"" + CONTENT_URI_SMS_INBOX + """;  
    45.         rtn += ","sent":"" + CONTENT_URI_SMS_SENT + """;  
    46.         rtn += ","conversations":"" + CONTENT_URI_SMS_CONVERSATIONS + """;  
    47.         rtn += "}";  
    48.         return rtn;  
    49.     }  
    50.   
    51.     public String get(int number) {  
    52.         return getData(null, number);  
    53.     }  
    54.   
    55.     public String getUnread(int number) {  
    56.         return getData("type=1 AND read=0", number);  
    57.     }  
    58.   
    59.     public String getRead(int number) {  
    60.         return getData("type=1 AND read=1", number);  
    61.     }  
    62.   
    63.     public String getInbox(int number) {  
    64.         return getData("type=1", number);  
    65.     }  
    66.   
    67.     public String getSent(int number) {  
    68.         return getData("type=2", number);  
    69.     }  
    70.   
    71.     public String getByThread(int thread) {  
    72.         return getData("thread_id=" + thread, 0);  
    73.     }  
    74.   
    75.     public String getData(String selection, int number) {  
    76.         Cursor cursor = null;  
    77.         ContentResolver contentResolver = mContext.getContentResolver();  
    78.         try {  
    79.             if (number > 0) {  
    80.                 cursor = contentResolver.query(Uri.parse(CONTENT_URI_SMS),  
    81.                         SMS_COLUMNS, selection, null, "date desc limit "  
    82.                                 + number);  
    83.             } else {  
    84.                 cursor = contentResolver.query(Uri.parse(CONTENT_URI_SMS),  
    85.                         SMS_COLUMNS, selection, null, "date desc");  
    86.             }  
    87.             if (cursor == null || cursor.getCount() == 0)  
    88.                 return "[]";  
    89.             String rtn = "";  
    90.             for (int i = 0; i < cursor.getCount(); i++) {  
    91.                 cursor.moveToPosition(i);  
    92.                 if (i > 0)  
    93.                     rtn += ",";  
    94.                 rtn += "{";  
    95.                 rtn += ""_id":" + cursor.getString(0);  
    96.                 rtn += ","thread_id":" + cursor.getString(1);  
    97.                 rtn += ","address":"" + cursor.getString(2) + """;  
    98.                 rtn += ","person":""  
    99.                         + ((cursor.getString(3) == null) ? "" : cursor  
    100.                                 .getString(3)) + """;  
    101.                 rtn += ","date":" + cursor.getString(4);  
    102.                 rtn += ","body":"" + cursor.getString(5) + """;  
    103.                 rtn += ","read":"  
    104.                         + ((cursor.getInt(6) == 1) ? "true" : "false");  
    105.                 rtn += ","type":" + cursor.getString(7);  
    106.                 rtn += ","service_center":" + cursor.getString(8);  
    107.                 rtn += "}";  
    108.             }  
    109.             return "[" + rtn + "]";  
    110.         } catch (Exception e) {  
    111.             return "[]";  
    112.         }  
    113.     }  
    114.   
    115.     public List<SMSBean> getThreads(int number) {  
    116.         Cursor cursor = null;  
    117.         ContentResolver contentResolver = mContext.getContentResolver();  
    118.         List<SMSBean> list = new ArrayList<SMSBean>();  
    119.         try {  
    120.             if (number > 0) {  
    121.                 cursor = contentResolver.query(  
    122.                         Uri.parse(CONTENT_URI_SMS_CONVERSATIONS),  
    123.                         THREAD_COLUMNS, null, null, "thread_id desc limit "  
    124.                                 + number);  
    125.             } else {  
    126.                 cursor = contentResolver.query(  
    127.                         Uri.parse(CONTENT_URI_SMS_CONVERSATIONS),  
    128.                         THREAD_COLUMNS, null, null, "date desc");  
    129.             }  
    130.             if (cursor == null || cursor.getCount() == 0)  
    131.                 return list;  
    132.             for (int i = 0; i < cursor.getCount(); i++) {  
    133.                 cursor.moveToPosition(i);  
    134.                 SMSBean mmt = new SMSBean(cursor.getString(0),  
    135.                         cursor.getString(1), cursor.getString(2));  
    136.                 list.add(mmt);  
    137.             }  
    138.             return list;  
    139.         } catch (Exception e) {  
    140.             return list;  
    141.         }  
    142.     }  
    143.   
    144.     public List<SMSBean> getThreadsNum(List<SMSBean> ll) {  
    145.   
    146.         Cursor cursor = null;  
    147.         ContentResolver contentResolver = mContext.getContentResolver();  
    148.         List<SMSBean> list = new ArrayList<SMSBean>();  
    149.         // try {  
    150.         for (SMSBean mmt : ll) {  
    151.             cursor = contentResolver.query(Uri.parse(CONTENT_URI_SMS),  
    152.                     SMS_COLUMNS, "thread_id = " + mmt.getThread_id(), null,  
    153.                     null);  
    154.             if (cursor == null || cursor.getCount() == 0)  
    155.                 return list;  
    156.             cursor.moveToFirst();  
    157.             mmt.setAddress(cursor.getString(2));  
    158.             mmt.setDate(cursor.getLong(4));  
    159.             mmt.setRead(cursor.getString(6));  
    160.             list.add(mmt);  
    161.         }  
    162.   
    163.         return list;  
    164.         // } catch (Exception e) {  
    165.         // Log.e("getThreadsNum", "getThreadsNum-------------");  
    166.         // return list;  
    167.         // }  
    168.     }  
    169. }  


     
     
    上面是短信列表的实现,下面是会话消息的实现
     
    界面布局:
    /Contact_Demo/res/layout/message_list_view.xml
    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:background="#000000" >  
    6.   
    7.     <ListView  
    8.         android:id="@+id/message_list"  
    9.         android:layout_width="fill_parent"  
    10.         android:layout_height="wrap_content"  
    11.         android:background="#00000000"  
    12.         android:cacheColorHint="#00000000"  
    13.         android:choiceMode="singleChoice"  
    14.         android:soundEffectsEnabled="true" />  
    15.   
    16. </RelativeLayout>  

    /Contact_Demo/res/layout/list_say_he_item.xml
    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="wrap_content"  
    5.     android:gravity="left"  
    6.     android:orientation="horizontal"  
    7.     android:paddingBottom="10dip"  
    8.     android:paddingLeft="5dip"  
    9.     android:paddingRight="50dip"  
    10.     android:paddingTop="10dip" >  
    11.   
    12.     <LinearLayout  
    13.         android:id="@+id/layout_bj"  
    14.         android:layout_width="wrap_content"  
    15.         android:layout_height="wrap_content"  
    16.         android:layout_gravity="left"  
    17.         android:background="@drawable/incoming"  
    18.         android:gravity="center_vertical"  
    19.         android:orientation="vertical" >  
    20.   
    21.         <com.suntek.contact.view.CustomTextView  
    22.             android:id="@+id/messagedetail_row_text"  
    23.             android:layout_width="wrap_content"  
    24.             android:layout_height="wrap_content"  
    25.             android:gravity="top|left"  
    26.             android:padding="5dip"  
    27.             android:textColor="#000000" />  
    28.   
    29.         <TextView  
    30.             android:id="@+id/messagedetail_row_date"  
    31.             android:layout_width="wrap_content"  
    32.             android:layout_height="wrap_content"  
    33.             android:layout_gravity="right"  
    34.             android:textAppearance="?android:attr/textAppearanceSmall"  
    35.             android:textColor="#65879e" />  
    36.     </LinearLayout>  
    37.   
    38. </LinearLayout>  
     
    /Contact_Demo/res/layout/list_say_me_item.xml
    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="wrap_content"  
    5.     android:gravity="right"  
    6.     android:orientation="horizontal"  
    7.     android:paddingBottom="10dip"  
    8.     android:paddingLeft="50dip"  
    9.     android:paddingRight="5dip"  
    10.     android:paddingTop="10dip" >  
    11.   
    12.     <LinearLayout  
    13.         android:id="@+id/layout_bj"  
    14.         android:layout_width="wrap_content"  
    15.         android:layout_height="wrap_content"  
    16.         android:layout_gravity="right"  
    17.         android:background="@drawable/outgoing"  
    18.         android:gravity="center_vertical"  
    19.         android:orientation="vertical" >  
    20.   
    21.         <com.suntek.contact.view.CustomTextView  
    22.             android:id="@+id/messagedetail_row_text"  
    23.             android:layout_width="wrap_content"  
    24.             android:layout_height="wrap_content"  
    25.             android:gravity="top|left"  
    26.             android:padding="5dip"  
    27.             android:textColor="#000000" />  
    28.   
    29.         <TextView  
    30.             android:id="@+id/messagedetail_row_date"  
    31.             android:layout_width="wrap_content"  
    32.             android:layout_height="wrap_content"  
    33.             android:layout_gravity="right"  
    34.             android:textAppearance="?android:attr/textAppearanceSmall"  
    35.             android:textColor="#65879e" />  
    36.     </LinearLayout>  
    37.   
    38. </LinearLayout>  


     
     
    短信消息实体类:
    /Contact_Demo/src/com/suntek/contact/model/MessageBean.java
    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. package com.suntek.contact.model;  
    2.   
    3. public class MessageBean {  
    4.   
    5.     private String name; // 联系人姓名  
    6.     private String date; // 日期  
    7.     private String text; // 文本  
    8.     private int layoutID; // 布局id,区分是发送人还是接收人  
    9.   
    10.     public MessageBean() {  
    11.     }  
    12.   
    13.     public MessageBean(String name, String date, String text, int layoutID) {  
    14.         super();  
    15.         this.name = name;  
    16.         this.date = date;  
    17.         this.text = text;  
    18.         this.layoutID = layoutID;  
    19.     }  
    20.   
    21.     public String getName() {  
    22.         return name;  
    23.     }  
    24.   
    25.     public void setName(String name) {  
    26.         this.name = name;  
    27.     }  
    28.   
    29.     public String getDate() {  
    30.         return date;  
    31.     }  
    32.   
    33.     public void setDate(String date) {  
    34.         this.date = date;  
    35.     }  
    36.   
    37.     public String getText() {  
    38.         return text;  
    39.     }  
    40.   
    41.     public void setText(String text) {  
    42.         this.text = text;  
    43.     }  
    44.   
    45.     public int getLayoutID() {  
    46.         return layoutID;  
    47.     }  
    48.   
    49.     public void setLayoutID(int layoutID) {  
    50.         this.layoutID = layoutID;  
    51.     }  
    52. }  
     
    消息列表适配器:
    /Contact_Demo/src/com/suntek/contact/adapter/MessageBoxListAdapter.java
    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. package com.suntek.contact.adapter;  
    2.   
    3. import java.util.List;  
    4.   
    5. import android.app.AlertDialog;  
    6. import android.content.Context;  
    7. import android.content.DialogInterface;  
    8. import android.graphics.Color;  
    9. import android.text.ClipboardManager;  
    10. import android.view.LayoutInflater;  
    11. import android.view.MotionEvent;  
    12. import android.view.View;  
    13. import android.view.View.OnClickListener;  
    14. import android.view.View.OnLongClickListener;  
    15. import android.view.View.OnTouchListener;  
    16. import android.view.ViewGroup;  
    17. import android.widget.BaseAdapter;  
    18. import android.widget.LinearLayout;  
    19. import android.widget.TextView;  
    20.   
    21. import com.suntek.contact.R;  
    22. import com.suntek.contact.model.MessageBean;  
    23.   
    24. /** 
    25.  * 消息列表适配器 
    26.  *  
    27.  * @author Administrator 
    28.  *  
    29.  */  
    30. public class MessageBoxListAdapter extends BaseAdapter {  
    31.   
    32.     private List<MessageBean> mbList;  
    33.     private Context ctx;  
    34.     private LinearLayout layout_father;  
    35.     private LayoutInflater vi;  
    36.     private LinearLayout layout_child;  
    37.     private TextView tvDate;  
    38.     private TextView tvText;  
    39.   
    40.     public MessageBoxListAdapter(Context context, List<MessageBean> coll) {  
    41.         ctx = context;  
    42.         vi = (LayoutInflater) ctx  
    43.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    44.         this.mbList = coll;  
    45.     }  
    46.   
    47.     public int getCount() {  
    48.         return mbList.size();  
    49.     }  
    50.   
    51.     public Object getItem(int position) {  
    52.         return mbList.get(position);  
    53.     }  
    54.   
    55.     public long getItemId(int position) {  
    56.         return position;  
    57.     }  
    58.   
    59.     public View getView(int position, View convertView, ViewGroup parent) {  
    60.   
    61.         MessageBean mb = mbList.get(position);  
    62.         // 得到布局ID  
    63.         int itemLayout = mb.getLayoutID();  
    64.         layout_father = new LinearLayout(ctx);  
    65.         vi.inflate(itemLayout, layout_father, true);  
    66.   
    67.         layout_father.setBackgroundColor(Color.TRANSPARENT);  
    68.         layout_child = (LinearLayout) layout_father  
    69.                 .findViewById(R.id.layout_bj);  
    70.   
    71.         tvText = (TextView) layout_father  
    72.                 .findViewById(R.id.messagedetail_row_text);  
    73.         tvText.setText(mb.getText());  
    74.   
    75.         tvDate = (TextView) layout_father  
    76.                 .findViewById(R.id.messagedetail_row_date);  
    77.         tvDate.setText(mb.getDate());  
    78.   
    79.         addListener(tvText, tvDate, layout_child, mb);  
    80.   
    81.         return layout_father;  
    82.     }  
    83.   
    84.     public void addListener(final TextView tvText, final TextView tvDate,  
    85.             LinearLayout layout_bj, final MessageBean mb) {  
    86.   
    87.         layout_bj.setOnClickListener(new OnClickListener() {  
    88.             public void onClick(View v) {  
    89.   
    90.             }  
    91.         });  
    92.   
    93.         layout_bj.setOnLongClickListener(new OnLongClickListener() {  
    94.             public boolean onLongClick(View v) {  
    95.                 tvText.setTextColor(0xffffffff);  
    96.                 showListDialog(newtan, mb);  
    97.                 return true;  
    98.             }  
    99.         });  
    100.   
    101.         layout_bj.setOnTouchListener(new OnTouchListener() {  
    102.             public boolean onTouch(View v, MotionEvent event) {  
    103.                 switch (event.getAction()) {  
    104.   
    105.                 case MotionEvent.ACTION_DOWN:  
    106.   
    107.                 case MotionEvent.ACTION_MOVE:  
    108.                     tvText.setTextColor(0xffffffff);  
    109.                     break;  
    110.   
    111.                 default:  
    112.                     tvText.setTextColor(Color.BLACK);  
    113.                     break;  
    114.                 }  
    115.                 return false;  
    116.             }  
    117.         });  
    118.     }  
    119.   
    120.     private String[] newtan = new String[] { "转发", "复制文本内容", "删除", "查询信息详情" };  
    121.   
    122.     private void showListDialog(final String[] arg, final MessageBean mb) {  
    123.         new AlertDialog.Builder(ctx).setTitle("信息选项")  
    124.                 .setItems(arg, new DialogInterface.OnClickListener() {  
    125.                     public void onClick(DialogInterface dialog, int which) {  
    126.                         switch (which) {  
    127.   
    128.                         case 0:  
    129.                             break;  
    130.   
    131.                         case 1:  
    132.                             ClipboardManager cmb = (ClipboardManager) ctx  
    133.                                     .getSystemService(ctx.CLIPBOARD_SERVICE);  
    134.                             cmb.setText(mb.getText());  
    135.                             break;  
    136.                         case 2:  
    137.   
    138.                             break;  
    139.                         case 3:  
    140.                             break;  
    141.                         }  
    142.                         ;  
    143.                     }  
    144.                 }).show();  
    145.     }  
    146. }  
     

    /Contact_Demo/src/com/suntek/contact/MessageBoxList.java
    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. package com.suntek.contact;  
    2.   
    3. import java.text.SimpleDateFormat;  
    4. import java.util.ArrayList;  
    5. import java.util.Date;  
    6. import java.util.List;  
    7.   
    8. import android.app.Activity;  
    9. import android.content.AsyncQueryHandler;  
    10. import android.content.ContentResolver;  
    11. import android.database.Cursor;  
    12. import android.net.Uri;  
    13. import android.os.Bundle;  
    14. import android.widget.ListView;  
    15. import android.widget.Toast;  
    16.   
    17. import com.suntek.contact.adapter.MessageBoxListAdapter;  
    18. import com.suntek.contact.model.MessageBean;  
    19.   
    20. /** 
    21.  * 短信消息列表 收发人 
    22.  *  
    23.  * @author Administrator 
    24.  *  
    25.  */  
    26. public class MessageBoxList extends Activity {  
    27.     private ListView talkView;  
    28.     private List<MessageBean> messages = null;  
    29.     private AsyncQueryHandler asyncQuery;  
    30.     private String address;  
    31.     private SimpleDateFormat sdf;  
    32.   
    33.     @Override  
    34.     protected void onCreate(Bundle savedInstanceState) {  
    35.         super.onCreate(savedInstanceState);  
    36.         setContentView(R.layout.message_list_view);  
    37.         sdf = new SimpleDateFormat("MM-dd HH:mm");  
    38.         String thread = getIntent().getStringExtra("threadId");  
    39.         init(thread);  
    40.     }  
    41.   
    42.     private void init(String thread) {  
    43.         asyncQuery = new MessageAsynQueryHandler(getContentResolver());  
    44.         talkView = (ListView) findViewById(R.id.message_list);  
    45.         messages = new ArrayList<MessageBean>();  
    46.   
    47.         Uri uri = Uri.parse("content://sms");  
    48.         String[] projection = new String[] { "date", "address", "person",  
    49.                 "body", "type" }; // 查询的列  
    50.         asyncQuery.startQuery(0, null, uri, projection,  
    51.                 "thread_id = " + thread, null, "date asc");  
    52.     }  
    53.   
    54.     /** 
    55.      * 异步查询数据库的类 
    56.      *  
    57.      * @author Administrator 
    58.      *  
    59.      */  
    60.     private class MessageAsynQueryHandler extends AsyncQueryHandler {  
    61.   
    62.         public MessageAsynQueryHandler(ContentResolver cr) {  
    63.             super(cr);  
    64.         }  
    65.   
    66.         @Override  
    67.         protected void onQueryComplete(int token, Object cookie, Cursor cursor) {  
    68.             if (cursor != null && cursor.getCount() > 0) {  
    69.                 cursor.moveToFirst();  
    70.                 for (int i = 0; i < cursor.getCount(); i++) {  
    71.                     cursor.moveToPosition(i);  
    72.                     String date = sdf.format(new Date(cursor.getLong(cursor  
    73.                             .getColumnIndex("date"))));  
    74.                     if (cursor.getInt(cursor.getColumnIndex("type")) == 1) {// 他说的信息  
    75.                         MessageBean d = new MessageBean(  
    76.                                 cursor.getString(cursor  
    77.                                         .getColumnIndex("address")),  
    78.                                 date,  
    79.                                 cursor.getString(cursor.getColumnIndex("body")),  
    80.                                 R.layout.list_say_he_item);  
    81.                         messages.add(d);  
    82.                     } else { // 我说的信息  
    83.                         MessageBean d = new MessageBean(  
    84.                                 cursor.getString(cursor  
    85.                                         .getColumnIndex("address")),  
    86.                                 date,  
    87.                                 cursor.getString(cursor.getColumnIndex("body")),  
    88.                                 R.layout.list_say_me_item);  
    89.                         messages.add(d);  
    90.                     }  
    91.                 }  
    92.                 if (messages.size() > 0) {  
    93.                     talkView.setAdapter(new MessageBoxListAdapter(  
    94.                             MessageBoxList.this, messages));  
    95.                     talkView.setDivider(null);  
    96.                     talkView.setSelection(messages.size());  
    97.                 } else {  
    98.                     Toast.makeText(MessageBoxList.this, "没有短信进行操作",  
    99.                             Toast.LENGTH_SHORT).show();  
    100.                 }  
    101.             }  
    102.             super.onQueryComplete(token, cookie, cursor);  
    103.         }  
    104.     }  
    105. }  
  • 相关阅读:
    【bzoj2653】【middle】【主席树+二分答案】
    Codeforces 464E. The Classic Problem
    关于主席树的入门,讲解和题单
    BZOJ3531-[Sdoi2014]旅行(树剖+线段树动态开点)
    [bzoj3123][洛谷P3302] [SDOI2013]森林(树上主席树+启发式合并)
    1018_两个圆相交的面积
    String对象中常用的方法
    张爱玲写的信
    React Native拆包及热更新方案 · Solartisan
    vue项目实战
  • 原文地址:https://www.cnblogs.com/mochaMM/p/5157291.html
Copyright © 2011-2022 走看看