android实现彩信接收的代码

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


 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值