android 浏览普通彩信列表核心源码

来源:互联网 发布:对接公众号源码 编辑:程序博客网 时间:2024/06/05 05:17

Activity 内容:

 

Java代码  收藏代码
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.io.InputStreamReader;  
  5. import java.io.UnsupportedEncodingException;  
  6. import java.text.SimpleDateFormat;  
  7. import java.util.Date;  
  8.   
  9. import android.database.Cursor;  
  10. import android.graphics.Bitmap;  
  11. import android.graphics.BitmapFactory;  
  12. import android.net.Uri;  
  13. import android.os.Bundle;  
  14. import android.provider.Contacts;  
  15.   
  16. import android.util.Log;  
  17. import android.view.View;  
  18. import android.widget.ImageView;  
  19. import android.widget.LinearLayout;  
  20. import android.widget.TextView;  
  21.   
  22. /** 
  23.  * @author mpqi  2012  
  24.  */  
  25. @SuppressWarnings("deprecation")  
  26. public class SmsPage extends Activity{  
  27.     private final String TAG="SmsPage";   
  28.       
  29.     private final Uri CONTENT_URI = Uri.parse("content://mms/inbox"); //查询彩信收件箱  
  30.       
  31.     private final Uri CONTENT_URI_PART = Uri.parse("content://mms/part"); //彩信附件表  
  32.       
  33.     public void onCreate(Bundle savedInstanceState){  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.main);  
  36.           
  37.         LinearLayout linearLayout = (LinearLayout) findViewById(R.id.content1);  
  38.           
  39.         Cursor cursor = getContentResolver().query(CONTENT_URI, nullnull,nullnull);  
  40.           
  41.         String name = "";  
  42.         while (cursor.moveToNext()) {  
  43.               
  44.             LinearLayout view = (LinearLayout) View.inflate(this, R.layout.smsitem_mms, null);  
  45.               
  46.             TextView address = (TextView)view.findViewById(R.id.sms_address);  
  47.             TextView body = (TextView)view.findViewById(R.id.sms_body);  
  48.             TextView date = (TextView)view.findViewById(R.id.sms_date);  
  49.             TextView sub = (TextView)view.findViewById(R.id.sms_sub);  
  50.             ImageView image = (ImageView)view.findViewById(R.id.sms_image);  
  51.   
  52.             SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  53.             Date time=new Date(cursor.getLong(cursor.getColumnIndex("date"))*1000);//彩信时间  
  54.             int id = cursor.getInt(cursor.getColumnIndex("_id"));//彩信Id  
  55.             String subject = cursor.getString(cursor.getColumnIndex("sub"));//彩信主题  
  56.             Cursor cAdd =null;  
  57.             Cursor cPhones=null;  
  58.             Cursor cPeople=null;  
  59.             Cursor cPart=null;  
  60.             try {  
  61.                 //根据彩信id从addr表中查出发送人电话号码,其中外键msg_id映射pdu表的id;  
  62.                 String selectionAdd = new String("msg_id=" + id );  
  63.                 Uri uriAddr = Uri.parse("content://mms/" + id + "/addr");  
  64.                 cAdd = getContentResolver().query(uriAddr, null, selectionAdd, nullnull);  
  65.                   
  66.                 //根据addr表中的电话号码在phones表中查出PERSON_ID,外键PERSON_ID映射people表中的_id  
  67.                 if(cAdd.moveToFirst()){//该处会得到2条记录,第一条记录为发件人号码,第二条为本机号码  
  68.                     String number= cAdd.getString(cAdd.getColumnIndex("address"));  
  69.                     cPhones = getContentResolver().query(Contacts.Phones.CONTENT_URI, new String[]{Contacts.Phones.PERSON_ID},Contacts.Phones.NUMBER +"=?",new String[]{number}, null);  
  70.                     if(cPhones.getCount()>0){//根据phones表中的PERSON_ID查出 people 表中联系人的名字  
  71.                         while (cPhones.moveToNext()) {  
  72.                             String pId = cPhones.getString(cPhones.getColumnIndex(Contacts.Phones.PERSON_ID));  
  73.                             Uri uriPeo = Uri.parse(Contacts.People.CONTENT_URI+"/"+pId);  
  74.                             cPeople = getContentResolver().query(uriPeo, null,null,nullnull);  
  75.                             if(cPeople.getCount()>0){  
  76.                                 String str="";  
  77.                                 while (cPeople.moveToNext()) {  
  78.                                     if(str == ""){  
  79.                                         str = cPeople.getString(cPeople.getColumnIndex(Contacts.People.DISPLAY_NAME));  
  80.                                     }else{  
  81.                                         str += ","+cPeople.getString(cPeople.getColumnIndex(Contacts.People.DISPLAY_NAME));  
  82.                                     }  
  83.                                 }  
  84.                                 name=number+"/"+str;//如果通讯录中存在,则以 ‘电话号码/名字’ 形式显示  
  85.                             }else{  
  86.                                 name=number;//如果是陌生人直接显示电话号码  
  87.                             }  
  88.                         }  
  89.                     }else{  
  90.                         name=number;//如果是陌生人直接显示电话号码  
  91.                     }      
  92.                 }  
  93.                   
  94.                 //根据彩信ID查询彩信的附件  
  95.                 String selectionPart = new String("mid="+id);//part表中的mid外键为pdu表中的_id  
  96.                 cPart = getContentResolver().query(CONTENT_URI_PART, null,selectionPart,nullnull);              
  97.                 String bodyStr="";  
  98.                 String[] coloumns = null;   
  99.                 String[] values = null;   
  100.                 while(cPart.moveToNext()){   
  101.                     coloumns = cPart.getColumnNames();   
  102.                     if(values == null)   
  103.                         values = new String[coloumns.length];   
  104.                     for(int i=0; i< cPart.getColumnCount(); i++){   
  105.                         values[i] = cPart.getString(i);   
  106.                     }   
  107.                     if(values[3].equals("image/jpeg") || values[3].equals("image/bmp") || values[3].equals("image/gif") || values[3].equals("image/jpg") || values[3].equals("image/png")){  //判断附件类型  
  108.                         image.setImageBitmap(getMmsImage(values[0]));//该处只会显示一张图片,如果有需求的朋友可以根据自己的需求将ImageView换成Gallery,修改一下方法  
  109.                         image.setVisibility(View.VISIBLE);  
  110.                     }else if(values[3].equals("text/plain")){  
  111.                         /**该处详细描述一下 
  112.                         *发现一个版本问题,之前用的2.1版本的多台手机测试通过,结果用1.6的G2报异常 
  113.                         *经过调试发现,1.6版本part表中根本就没有"text"列,也就是values[13],所以就 
  114.                         *报错了,好像在1.6版本(我只测过G2,嘿嘿),就算是文本信息也是以一个附件形 
  115.                         *式存在_date里面也就是values[12]里面,与图片类似,但在2.1里面却不是这样存 
  116.                         *的,文本信息被存在了"text"这个字段中,且"_date"为null*/  
  117.   
  118.                         if(values[12]!=null){//所以该处需判断一下,如果_date为null,可直接设置内容为"text"  
  119.                             bodyStr=getMmsText(values[0]);  
  120.                         }else{  
  121.                             bodyStr = values[13];                             
  122.                         }  
  123.                     }  
  124.                 }  
  125.                 if(!"".equals(subject) && subject != null){  
  126.                     try {  
  127.                         sub.setText(new String(subject.getBytes("iso-8859-1"),"UTF-8"));//设置彩信主题的编码格式  
  128.                     } catch (UnsupportedEncodingException e) {  
  129.                         e.printStackTrace();  
  130.                     }  
  131.                 }  
  132.                 if(!"".equals(bodyStr)){  
  133.                     body.setText(bodyStr);  
  134.                 }  
  135.                 address.setText(name);   
  136.                 date.setText(format.format(time));  
  137.             } catch (RuntimeException e) {  
  138.                 Log.e(TAG, e.getMessage());  
  139.             }finally{  
  140.                 if(cAdd != null){  
  141.                     cAdd.close();  
  142.                 }  
  143.                 if(cPart != null){  
  144.                     cPart.close();  
  145.                 }  
  146.                 if(cPeople != null){  
  147.                     cPeople.close();  
  148.                 }  
  149.                 if(cPhones != null){  
  150.                     cPhones.close();  
  151.                 }  
  152.             }  
  153.              
  154.             linearLayout.addView(view);  
  155.               
  156.         }  
  157.           
  158.           
  159.     }  
  160.       
  161.       
  162.     private String getMmsText(String _id){ //读取文本附件  
  163.         Uri partURI = Uri.parse("content://mms/part/" + _id );   
  164.         InputStream is = null;   
  165.         StringBuilder sb = new StringBuilder();  
  166.         try {   
  167.             is = getContentResolver().openInputStream(partURI);   
  168.             if(is!=null){  
  169.                 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));  
  170.                 String temp = reader.readLine();  
  171.                 while (temp != null) {  
  172.                     sb.append(temp);  
  173.                     temp=reader.readLine();//在网上看到很多把InputStream转成string的文章,没有这关键的一句,几乎千遍一律的复制粘贴,该处如果不加上这句的话是会内存溢出的  
  174.                 }  
  175.             }  
  176.         }catch (IOException e) {   
  177.             e.printStackTrace();    
  178.             Log.v(TAG, "读取附件异常"+e.getMessage());  
  179.         }finally{   
  180.             if (is != null){   
  181.                 try {   
  182.                     is.close();   
  183.                 }catch (IOException e){  
  184.                     Log.v(TAG, "读取附件异常"+e.getMessage());  
  185.                 }  
  186.             }   
  187.         }  
  188.         return sb.toString();  
  189.     }  
  190.       
  191.     private Bitmap getMmsImage(String _id){ //读取图片附件  
  192.         Uri partURI = Uri.parse("content://mms/part/" + _id );   
  193.         //ByteArrayOutputStream baos = new ByteArrayOutputStream();   
  194.         InputStream is = null;   
  195.         Bitmap bitmap=null;  
  196.         try {   
  197.             is = getContentResolver().openInputStream(partURI);   
  198.             //byte[] buffer = new byte[256];    
  199.             //int len = -1;  
  200.             //while ((len = is.read(buffer)) != -1) {  
  201.             //    baos.write(buffer, 0, len);  
  202.             //}  
  203.             //bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.toByteArray().length);  
  204.             bitmap = BitmapFactory.decodeStream(is);  
  205.         }catch (IOException e) {   
  206.             e.printStackTrace();    
  207.             Log.v(TAG, "读取图片异常"+e.getMessage());  
  208.         }finally{   
  209.             if (is != null){   
  210.                 try {   
  211.                     is.close();   
  212.                 }catch (IOException e){  
  213.                     Log.v(TAG, "读取图片异常"+e.getMessage());  
  214.                 }  
  215.             }   
  216.         }  
  217.         return bitmap;  
  218.     }  
  219. }