Android中读取短信信息 – OPEN 开发经验库

Android中读取的短信文件有

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 * 所有的短信
 */
public static final String SMS_URI_ALL = "content://sms/";
/**
 * 收件箱短信
 */
public static final String SMS_URI_INBOX = "content://sms/inbox";
/**
 * 发件箱短信
 */
public static final String SMS_URI_SEND = "content://sms/sent";
/**
 * 草稿箱短信
 */
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来提取各种信息如下:

?
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
package com.pei.info;
/**
 * class name:SmsInfo<BR>
 * class description:获取短信各种信息的类<BR>
 * PS: <BR>
 * Date:2012-3-19<BR>
 *
 * @version 1.00
 * @author CODYY)peijiangping
 */
public class SmsInfo {
    /**
     * 短信内容
     */
    private String smsbody;
    /**
     * 发送短信的电话号码
     */
    private String phoneNumber;
    /**
     * 发送短信的日期和时间
     */
    private String date;
    /**
     * 发送短信人的姓名
     */
    private String name;
    /**
     * 短信类型1是接收到的,2是已发出
     */
    private String type;
    public String getSmsbody() {
        return smsbody;
    }
    public void setSmsbody(String smsbody) {
        this.smsbody = smsbody;
    }
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
}

然后就是封装类,读取信息内容SmsContent.java

?
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
package com.pei.util;
import java.util.ArrayList;
import java.util.List;
import com.pei.info.SmsInfo;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
/**
 * class name:SmsChoose<BR>
 * class description:获取手机中的各种短信信息<BR>
 * PS: 需要权限 <uses-permission android:name="android.permission.READ_SMS" /><BR>
 * Date:2012-3-19<BR>
 *
 * @version 1.00
 * @author CODYY)peijiangping
 */
public class SmsContent {
    private Activity activity;//这里有个activity对象,不知道为啥以前好像不要,现在就要了。自己试试吧。
    private Uri uri;
    List<SmsInfo> infos;
    public SmsContent(Activity activity, Uri uri) {
        infos = new ArrayList<SmsInfo>();
        this.activity = activity;
        this.uri = uri;
    }
    /**
     * Role:获取短信的各种信息 <BR>
     * Date:2012-3-19 <BR>
     *
     * @author CODYY)peijiangping
     */
    public List<SmsInfo> getSmsInfo() {
        String[] projection = new String[] { "_id", "address", "person",
                "body", "date", "type" };
        Cursor cusor = activity.managedQuery(uri, projection, null, null,
                "date desc");
        int nameColumn = cusor.getColumnIndex("person");
        int phoneNumberColumn = cusor.getColumnIndex("address");
        int smsbodyColumn = cusor.getColumnIndex("body");
        int dateColumn = cusor.getColumnIndex("date");
        int typeColumn = cusor.getColumnIndex("type");
        if (cusor != null) {
            while (cusor.moveToNext()) {
                SmsInfo smsinfo = new SmsInfo();
                smsinfo.setName(cusor.getString(nameColumn));
                smsinfo.setDate(cusor.getString(dateColumn));
                smsinfo.setPhoneNumber(cusor.getString(phoneNumberColumn));
                smsinfo.setSmsbody(cusor.getString(smsbodyColumn));
                smsinfo.setType(cusor.getString(typeColumn));
                infos.add(smsinfo);
            }
            cusor.close();
        }
        return infos;
    }
}

在提供一个listview来显示短信内容:

?
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
package com.pei.activity;
import java.util.List;
import com.pei.fixed.AllFinalInfo;
import com.pei.info.SmsInfo;
import com.pei.util.SmsContent;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
 * class name:SmsListActivity<BR>
 * class description:显示短信的列表<BR>
 * PS: <BR>
 * Date:2012-3-19<BR>
 *
 * @version 1.00
 * @author CODYY)peijiangping
 */
public class SmsListActivity extends Activity {
    private ListView listview;
    private List<SmsInfo> infos;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms);
        Uri uri = Uri.parse(AllFinalInfo.SMS_URI_INBOX);
        SmsContent sc = new SmsContent(this, uri);
        infos = sc.getSmsInfo();
        listview = (ListView) this.findViewById(R.id.ListView_Sms);
        listview.setAdapter(new SmsListAdapter(this));
    }
    class SmsListAdapter extends BaseAdapter {
        private LayoutInflater layoutinflater;
        private View myview;
        public SmsListAdapter(Context c) {
            layoutinflater = LayoutInflater.from(c);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return infos.size();
        }
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                myview = layoutinflater.inflate(R.layout.smsitem, null);
            }
            TextView body = (TextView) myview
                    .findViewById(R.id.TextView_SmsBody);
            TextView name = (TextView) myview
                    .findViewById(R.id.TextView_SmsName);
            body.setText(infos.get(position).getSmsbody());
            name.setText(infos.get(position).getName());
            return myview;
        }
    }
}

这样你就可以随意的读取各种短信的各种内容了,显示也十分方便。方法还可以继续封装。有待改进。

来源URL:http://www.open-open.com/lib//view/open1332139318999.html