Android中读取短信信息
16lz
2021-01-23
Android中读取的短信文件有
01 | /** |
02 |
* 所有的短信 |
03 |
*/ |
04 | public static final String SMS_URI_ALL = "content://sms/" ; |
05 | /** |
06 |
* 收件箱短信 |
07 |
*/ |
08 | public static final String SMS_URI_INBOX = "content://sms/inbox" ; |
09 | /** |
10 |
* 发件箱短信 |
11 |
*/ |
12 | public static final String SMS_URI_SEND = "content://sms/sent" ; |
13 | /** |
14 |
* 草稿箱短信 |
15 |
*/ |
16 | public static final String SMS_URI_DRAFT = "content://sms/draft" ; |
读取的短信信息有:
_id:短信序号,如100
* thread_id:对话的序号,如100,与同一个手机号互发的短信,其序号是相同的
* address:发件人地址,即手机号,如+8613811810000
* person:发件人,如果发件人在通讯录中则为具体姓名,陌生人为null
* date:日期,long型,如1256539465022,可以对日期显示格式进行设置
* protocol:协议0SMS_RPOTO短信,1MMS_PROTO彩信 read:是否阅读0未读,1已读
* status:短信状态-1接收,0complete,64pending,128failed
* type:短信类型1是接收到的,2是已发出 body:短信具体内容
* service_center:短信服务中心号码编号,如+8613800755500
下面我们可以先建立一个SmsInfo来提取各种信息如下:
01 | package com.pei.info; |
02 |
03 | /** |
04 |
* class name:SmsInfo<BR> |
05 |
* class description:获取短信各种信息的类<BR> |
06 |
* PS: <BR> |
07 |
* Date:2012-3-19<BR> |
08 |
* |
09 |
* @version 1.00 |
10 |
* @author CODYY)peijiangping |
11 |
*/ |
12 | public class SmsInfo { |
13 |
/** |
14 |
* 短信内容 |
15 |
*/ |
16 |
private String smsbody; |
17 |
/** |
18 |
* 发送短信的电话号码 |
19 |
*/ |
20 |
private String phoneNumber; |
21 |
/** |
22 |
* 发送短信的日期和时间 |
23 |
*/ |
24 |
private String date; |
25 |
/** |
26 |
* 发送短信人的姓名 |
27 |
*/ |
28 |
private String name; |
29 |
/** |
30 |
* 短信类型1是接收到的,2是已发出 |
31 |
*/ |
32 |
private String type; |
33 |
34 |
public String getSmsbody() { |
35 |
return smsbody; |
36 |
} |
37 |
38 |
public void setSmsbody(String smsbody) { |
39 |
this .smsbody = smsbody; |
40 |
} |
41 |
42 |
public String getPhoneNumber() { |
43 |
return phoneNumber; |
44 |
} |
45 |
46 |
public void setPhoneNumber(String phoneNumber) { |
47 |
this .phoneNumber = phoneNumber; |
48 |
} |
49 |
50 |
public String getDate() { |
51 |
return date; |
52 |
} |
53 |
54 |
public void setDate(String date) { |
55 |
this .date = date; |
56 |
} |
57 |
58 |
public String getName() { |
59 |
return name; |
60 |
} |
61 |
62 |
public void setName(String name) { |
63 |
this .name = name; |
64 |
} |
65 |
66 |
public String getType() { |
67 |
return type; |
68 |
} |
69 |
70 |
public void setType(String type) { |
71 |
this .type = type; |
72 |
} |
73 | } |
然后就是封装类,读取信息内容SmsContent.java
01 | package com.pei.util; |
02 |
03 | import java.util.ArrayList; |
04 | import java.util.List; |
05 |
06 | import com.pei.info.SmsInfo; |
07 |
08 | import android.app.Activity; |
09 | import android.database.Cursor; |
10 | import android.net.Uri; |
11 |
12 | /** |
13 |
* class name:SmsChoose<BR> |
14 |
* class description:获取手机中的各种短信信息<BR> |
15 |
* PS: 需要权限 <uses-permission android:name="android.permission.READ_SMS" /><BR> |
16 |
* Date:2012-3-19<BR> |
17 |
* |
18 |
* @version 1.00 |
19 |
* @author CODYY)peijiangping |
20 |
*/ |
21 | public class SmsContent { |
22 |
private Activity activity; //这里有个activity对象,不知道为啥以前好像不要,现在就要了。自己试试吧。 |
23 |
private Uri uri; |
24 |
List<SmsInfo> infos; |
25 |
26 |
public SmsContent(Activity activity, Uri uri) { |
27 |
infos = new ArrayList<SmsInfo>(); |
28 |
this .activity = activity; |
29 |
this .uri = uri; |
30 |
} |
31 |
32 |
/** |
33 |
* Role:获取短信的各种信息 <BR> |
34 |
* Date:2012-3-19 <BR> |
35 |
* |
36 |
* @author CODYY)peijiangping |
37 |
*/ |
38 |
public List<SmsInfo> getSmsInfo() { |
39 |
String[] projection = new String[] { "_id" , "address" , "person" , |
40 |
"body" , "date" , "type" }; |
41 |
Cursor cusor = activity.managedQuery(uri, projection, null , null , |
42 |
"date desc" ); |
43 |
int nameColumn = cusor.getColumnIndex( "person" ); |
44 |
int phoneNumberColumn = cusor.getColumnIndex( "address" ); |
45 |
int smsbodyColumn = cusor.getColumnIndex( "body" ); |
46 |
int dateColumn = cusor.getColumnIndex( "date" ); |
47 |
int typeColumn = cusor.getColumnIndex( "type" ); |
48 |
if (cusor != null ) { |
49 |
while (cusor.moveToNext()) { |
50 |
SmsInfo smsinfo = new SmsInfo(); |
51 |
smsinfo.setName(cusor.getString(nameColumn)); |
52 |
smsinfo.setDate(cusor.getString(dateColumn)); |
53 |
smsinfo.setPhoneNumber(cusor.getString(phoneNumberColumn)); |
54 |
smsinfo.setSmsbody(cusor.getString(smsbodyColumn)); |
55 |
smsinfo.setType(cusor.getString(typeColumn)); |
56 |
infos.add(smsinfo); |
57 |
} |
58 |
cusor.close(); |
59 |
} |
60 |
return infos; |
61 |
} |
62 | } |
在提供一个listview来显示短信内容:
01 | package com.pei.activity; |
02 |
03 | import java.util.List; |
04 |
05 | import com.pei.fixed.AllFinalInfo; |
06 | import com.pei.info.SmsInfo; |
07 | import com.pei.util.SmsContent; |
08 | import android.app.Activity; |
09 | import android.content.Context; |
10 | import android.net.Uri; |
11 | import android.os.Bundle; |
12 | import android.view.LayoutInflater; |
13 | import android.view.View; |
14 | import android.view.ViewGroup; |
15 | import android.widget.BaseAdapter; |
16 | import android.widget.ListView; |
17 | import android.widget.TextView; |
18 |
19 | /** |
20 |
* class name:SmsListActivity<BR> |
21 |
* class description:显示短信的列表<BR> |
22 |
* PS: <BR> |
23 |
* Date:2012-3-19<BR> |
24 |
* |
25 |
* @version 1.00 |
26 |
* @author CODYY)peijiangping |
27 |
*/ |
28 | public class SmsListActivity extends Activity { |
29 |
private ListView listview; |
30 |
private List<SmsInfo> infos; |
31 |
32 |
@Override |
33 |
protected void onCreate(Bundle savedInstanceState) { |
34 |
super .onCreate(savedInstanceState); |
35 |
setContentView(R.layout.sms); |
36 |
Uri uri = Uri.parse(AllFinalInfo.SMS_URI_INBOX); |
37 |
SmsContent sc = new SmsContent( this , uri); |
38 |
infos = sc.getSmsInfo(); |
39 |
listview = (ListView) this .findViewById(R.id.ListView_Sms); |
40 |
listview.setAdapter( new SmsListAdapter( this )); |
41 |
} |
42 |
43 |
class SmsListAdapter extends BaseAdapter { |
44 |
private LayoutInflater layoutinflater; |
45 |
private View myview; |
46 |
47 |
public SmsListAdapter(Context c) { |
48 |
layoutinflater = LayoutInflater.from(c); |
49 |
} |
50 |
51 |
@Override |
52 |
public int getCount() { |
53 |
// TODO Auto-generated method stub |
54 |
return infos.size(); |
55 |
} |
56 |
57 |
@Override |
58 |
public Object getItem( int position) { |
59 |
// TODO Auto-generated method stub |
60 |
return null ; |
61 |
} |
62 |
63 |
@Override |
64 |
public long getItemId( int position) { |
65 |
// TODO Auto-generated method stub |
66 |
return 0 ; |
67 |
} |
68 |
69 |
@Override |
70 |
public View getView( int position, View convertView, ViewGroup parent) { |
71 |
if (convertView == null ) { |
72 |
myview = layoutinflater.inflate(R.layout.smsitem, null ); |
73 |
} |
74 |
TextView body = (TextView) myview |
75 |
.findViewById(R.id.TextView_SmsBody); |
76 |
TextView name = (TextView) myview |
77 |
.findViewById(R.id.TextView_SmsName); |
78 |
body.setText(infos.get(position).getSmsbody()); |
79 |
name.setText(infos.get(position).getName()); |
80 |
return myview; |
81 |
} |
82 |
83 |
} |
84 | } |
更多相关文章
- android中读取短信
- execlp启动android进程命令窗口通过adb shell 进入android 的Lin
- 获取Android 设备信息——build.prop
- android调用隐藏的网络信息设置菜单实现
- 《Android/OPhone开发完全讲义》连载(5):Notification与状态栏信息
- 通过android代码获取android系统的imei、手机型号、手机品牌、an
- android 通过广播获取指定联系人短信内容
- Android获取所有安装APP信息的详细代码
- android 音乐信息乱码处理