通过ContentProvider拿到手机所有短信

原理是通过,contentprovider获取系统短信数据库中的字段信息而达到获取内容目的

示例图片


具体代码如下:

import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ScrollView;
import android.widget.TextView;

import java.sql.Date;
import java.text.SimpleDateFormat;


public class SmsRead2 extends AppCompatActivity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView textView = new TextView(this);
        textView.setText(getSms());

        ScrollView scrollView = new ScrollView(this);
        scrollView.addView(textView);

        setContentView(scrollView);
    }

    public String getSms(){

        StringBuilder stringBuilder =  new StringBuilder();

        try{

            //全部短信的路径
            Uri uri = Uri.parse("content://sms/");
            String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
            //获取手机内部短信
            Cursor cur = getContentResolver().query(uri, projection, null, null, "date desc");

            if (cur.moveToFirst()) {

                int index_Address = cur.getColumnIndex("address");  //手机号码
                int index_Body = cur.getColumnIndex("body");        //短信内容
                int index_Date = cur.getColumnIndex("date");        //发件日期
                int index_Type = cur.getColumnIndex("type");        //短信状态

                do {

                    String strAddress = cur.getString(index_Address);   //得到的手机号码
                    String strbody = cur.getString(index_Body);         //得到的短信内容
                    long longDate = cur.getLong(index_Date);            //得到的发件日期
                    int intType = cur.getInt(index_Type);               //得到的短信状态


                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    Date date = new Date(longDate);
                    String strDate = dateFormat.format(date);

                    String strType = "";
                    if (intType == 1) {
                        strType = "接收";
                    } else if (intType == 2) {
                        strType = "发送";
                    } else {
                        strType = "草稿";
                    }

                    stringBuilder.append("[ 发/收件人号码:");
                    stringBuilder.append(strAddress + "短信内容:");
                    stringBuilder.append(strbody + "发/收信时间:");
                    stringBuilder.append(strDate + "信息状态:");
                    stringBuilder.append(strType);
                    stringBuilder.append(" ]\n\n");
                } while (cur.moveToNext());

                if (!cur.isClosed()) {
                    cur.close();
                    cur = null;
                }
            } else {
                stringBuilder.append("no result!");
            }

            stringBuilder.append("getSmsInPhone has executed!");

        }catch (SQLiteException ex){
            ex.printStackTrace();
        }

        return stringBuilder.toString();

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值