Android development series: how to get the content of mobile phone text messages

1 The relevant fields of the SMS database in the mobile phone include:
_id: the serial number of the SMS, such as 100   
address: the sender's address, that is, the mobile phone number, such as +8613811810000, here you need to pay attention to whether the mobile phone number is added with +86, you can add 86 or not 86 separately test  
person: the sender, if the sender is in the address book, it is the specific name, the stranger is null   
date: date, long type, such as 1256539465022, you can set the date display format, here is done with sqlite statement When filtering, the unit of the date field is milliseconds, and the code to get the system millisecond time is:

long totalMilliSeconds = System.currentTimeMillis();   

Protocol: Protocol 0SMS_RPOTO SMS, 1MMS_PROTO MMS      
read: Whether to read 0 unread, 1 read   
status: SMS status-1 received, 0complete, 64pending, 128failed   
type: SMS type 1 is received, 2 is sent      
body: SMS specific Content
The uri to read the content of the SMS is:
Uri uri = Uri.parse("content://sms"); //Read all SMS
Uri uri = Uri.parse("content://sms/inbox"); / /Read inbox SMS

2 Add permissions to the manifest file to obtain read and write permissions for SMS:

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

3 The core code is:

private void checkSMSPermission() {
    
    
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
                != PackageManager.PERMISSION_GRANTED) {
    
    
            //未获取到读取短信权限
            //向系统申请权限
            ActivityCompat.requestPermissions(this,
                    new String[]{
    
    Manifest.permission.READ_SMS}, REQ_CODE_CONTACT);
        } else {
    
    
           //执行自己的程序 
        }
    }

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    
    
        //判断用户是否,同意 获取短信授权
        if (requestCode == REQ_CODE_CONTACT && grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
    
            //获取到读取短信权限
            //执行自己的程序
        } else {
    
    
            Toast.makeText(this, "未获取到短信权限", Toast.LENGTH_SHORT).show();
        }
    }

private void query() {
    
    
        //读取所有短信
        Uri uri = Uri.parse("content://sms/");
        ContentResolver resolver = getContentResolver();
        //查询条件可以根据实际业务填写,这里均为null,代表没有筛选条件
        //其中第三个参数代表where条件
        //第五个参数代表order by
        //第四个参数根据第三个参数来写,如果你在第三个参数里面有?,那么你在第四个写的数据就会替换掉?
        Cursor cursor = resolver.query(uri, new String[]{
    
    "_id", "address", "body", "date", "type"}, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
    
    
            String body;
            while (cursor.moveToNext()) {
    
    
               //存储短信内容
               body = cursor.getString(2);
            }
        }
    }
//补充:
cursor.close();//关闭游标,且释放资源
cursor.isClosed();//如果为TRUE表示该游标已关闭
cursor.getColumnCount();//返回所有列的总数
cursor.getColumnNames();//返回一个字符串数组的列名,即将列名全部返回到一个字符串数组中
cursor.getColumnName(columnIndex);//从给定的索引返回列名
cursor.getCount();//返回Cursor中的行数
cursor.moveToFirst();//移动光标到第一行
cursor.moveToLast();//移动光标到最后一行
cursor.moveToNext();//移动光标到下一行
cursor.moveToPrevious();//移动光标到上一行
cursor.moveToPosition(position);//移动光标到给定位置

Reference materials:

https://blog.csdn.net/qq_42179105/article/details/82929521?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.controldepth&dist_request_id=8d7d5f2b-1b3f-4f66-but5d1-e89d3979bb pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.control Android studio Get the content of mobile phone text messages and output and display

https://blog.csdn.net/chenliguan/article/details/48316585 Android advanced reading mobile phone short message display listview

https://blog.csdn.net/ALittleForward/article/details/40480389?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&dist_request_id=1f38157e-2fdc-42f8-ab73-f2990-distributedepth_1 pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control SMS application development based on Android

Guess you like

Origin blog.csdn.net/langxiaolin/article/details/114261336