美文网首页
智能终端软件开发——查看系统短信

智能终端软件开发——查看系统短信

作者: itczt | 来源:发表于2019-05-30 22:26 被阅读0次

    1.创建程序

    创建一个名为ReadSMS的应用程序,指定包名为package com.czt.readsms;设计用户交互界面,预览效果如图:



    对应的的布局代码如下:

    activity_main.xml

    package com.czt.readsms;
    
    import android.content.ContentResolver;
    import android.database.Cursor;
    import android.net.Uri;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
        private TextView tvSms;
        private TextView tvDes;
        private String text = "";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tvSms = (TextView)findViewById(R.id.tv_sms);
            tvDes = (TextView)findViewById(R.id.tv_des);
            }
        //单击Button时触发的方法
        public void readSMS(View view){
            //查询系统的信息uri
            Uri uri = Uri.parse("content://sms/");
            //获取ContentResolver对象
            ContentResolver resolver = getContentResolver();
            //通过ContentResolver对象查询系统短息
          Cursor cursor = resolver.query(uri,new String[]{"_id","address","type","body","date"},null,null,null);
            List<SmsInfo> smsInfos = new ArrayList<SmsInfo>();
            if (cursor != null && cursor.getCount()>0){
                tvDes.setVisibility(View.VISIBLE);
                while (cursor.moveToNext()){
                    int _id = cursor.getInt(0);
                    String address = cursor.getString(1);
                    int type = cursor.getInt(2);
                    String body = cursor.getString(3);
                    long date = cursor.getLong(4);
                    SmsInfo smsInfo = new SmsInfo(_id,address,type,body,date);
                    smsInfos.add(smsInfo);
                }
                cursor.close();
            }
    
            for (int i =0;i<smsInfos.size();i++){
                text +="手机号码:"+smsInfos.get(i).getAddress()+"\n";
                text +="短信内容:"+smsInfos.get(i).getBody()+"\n\n";
                tvSms.setText(text);
            }
        }
    }
    

    在上述代码中,设置了一个Button,定义了它的单击事件“readSMS”,单击按钮之后将查询到的短信信息显示到TextView中。

    编写实体类

    接下来创建一个实体类SmsInfo,用于封装短信的属性,存储单条短信的信息,它的成员变量为_id,dete,,type,body和address,具体代码如下:
    Smsinfo.java

    package com.czt.readsms;
    
    public class SmsInfo {
        private int _id; //短信的主键
        private String address; //发送地址
        private int type; //类型
        private String body; //短信内容
        private long date; //时间
        public SmsInfo(int _id,String address,int type,String body,long date){
            this._id = _id;
            this.address = address;
            this.type = type;
            this.body = body;
            this.date = date;
        }
    
        public int get_id() {
            return _id;
        }
    
        public void set_id(int _id) {
            this._id = _id;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        public int getType() {
            return type;
        }
    
        public void setType(int type) {
            this.type = type;
        }
    
        public String getBody() {
            return body;
        }
    
        public void setBody(String body) {
            this.body = body;
        }
    
        public long getDate() {
            return date;
        }
    
        public void setDate(long date) {
            this.date = date;
        }
    }
    
    

    3.编写界面交互代码
    在MainActivity中编写交互代码,以实现查看系统短信的功能,具体代码如下:

    package com.czt.readsms;
    
    import android.content.ContentResolver;
    import android.database.Cursor;
    import android.net.Uri;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
        private TextView tvSms;
        private TextView tvDes;
        private String text = "";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tvSms = (TextView)findViewById(R.id.tv_sms);
            tvDes = (TextView)findViewById(R.id.tv_des);
            }
        //单击Button时触发的方法
        public void readSMS(View view){
            //查询系统的信息uri
            Uri uri = Uri.parse("content://sms/");
            //获取ContentResolver对象
            ContentResolver resolver = getContentResolver();
            //通过ContentResolver对象查询系统短息
          Cursor cursor = resolver.query(uri,new String[]{"_id","address","type","body","date"},null,null,null);
            List<SmsInfo> smsInfos = new ArrayList<SmsInfo>();
            if (cursor != null && cursor.getCount()>0){
                tvDes.setVisibility(View.VISIBLE);
                while (cursor.moveToNext()){
                    int _id = cursor.getInt(0);
                    String address = cursor.getString(1);
                    int type = cursor.getInt(2);
                    String body = cursor.getString(3);
                    long date = cursor.getLong(4);
                    SmsInfo smsInfo = new SmsInfo(_id,address,type,body,date);
                    smsInfos.add(smsInfo);
                }
                cursor.close();
            }
    
            for (int i =0;i<smsInfos.size();i++){
                text +="手机号码:"+smsInfos.get(i).getAddress()+"\n";
                text +="短信内容:"+smsInfos.get(i).getBody()+"\n\n";
                tvSms.setText(text);
            }
        }
    }
    

    在上述代码中,实现了利用ContentResolver读取系统短信功能,单击Button按钮将读取到的短信显示到主页面上。需要注意的是,在使用完Cursor之后,一定要关闭,否则会造成内存泄漏。

    4.添加权限

    该案例进行了读取短信的操作,因此需要在AndroidMainfest。xml文件中加上读取的短信的权限,代码如下:

     <uses-permission android:name="android.permission.READ_SMS"/>
    

    相关文章

      网友评论

          本文标题:智能终端软件开发——查看系统短信

          本文链接:https://www.haomeiwen.com/subject/wicbtctx.html