欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android利用ContentProvider读取短信内容

程序员文章站 2022-06-25 20:37:10
本文实例为大家分享了android利用contentprovider读取短信内容的具体代码,供大家参考,具体内容如下首先,我们来看下运行效果运行效果如下:展示短信内容的效果如下:布局文件(activi...

本文实例为大家分享了android利用contentprovider读取短信内容的具体代码,供大家参考,具体内容如下

首先,我们来看下运行效果

运行效果如下:

Android利用ContentProvider读取短信内容

展示短信内容的效果如下:

Android利用ContentProvider读取短信内容

布局文件(activity_sms.xml)

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_sms"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingbottom="@dimen/activity_vertical_margin"
    android:paddingleft="@dimen/activity_horizontal_margin"
    android:paddingright="@dimen/activity_horizontal_margin"
    android:paddingtop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.administrator.myapplication.smsactivity">

    <button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取手机所有的短信内容"
        android:onclick="getcontactssms"
        />
    <listview
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lv_sms_list"

        ></listview>
</linearlayout>

一个简单的读取短信内容的例子(smsactivity)

package com.example.administrator.myapplication;

import android.content.contentresolver;
import android.database.cursor;
import android.net.uri;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.widget.listview;
import android.widget.simpleadapter;

import java.text.simpledateformat;
import java.util.arraylist;
import java.util.date;
import java.util.hashmap;
import java.util.list;
import java.util.map;

public class smsactivity extends appcompatactivity {

    private listview lv_sms_list;
    private list<map<string,object>> data;
    private simpleadapter simpleadapter;
    private contentresolver contentresolver;

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_sms);

        //获取访问者
        contentresolver = getcontentresolver();

        lv_sms_list = (listview) findviewbyid(r.id.lv_sms_list);
        data = new arraylist<map<string,object>>();
        //适配器
        simpleadapter = new simpleadapter(this,data,android.r.layout.simple_list_item_2,new string[]{"names","note"},new int[]{android.r.id.text1,android.r.id.text2});
        lv_sms_list.setadapter(simpleadapter);
    }


    public void getcontactssms(view view) {
        //读取所有短信
        uri uri=uri.parse("content://sms/");
        contentresolver resolver = getcontentresolver();
        cursor cursor = resolver.query(uri, new string[]{"_id", "address", "body", "date", "type"}, null, null, null);
        if(cursor!=null&&cursor.getcount()>0){
            int _id;
            string address;
            string body;
            string date;
            int type;
            while (cursor.movetonext()){
                map<string,object>map=new hashmap<string,object>();
                _id=cursor.getint(0);
                address=cursor.getstring(1);
                body=cursor.getstring(2);
                date=cursor.getstring(3);
                type=cursor.getint(4);
                map.put("names",body);
                data.add(map);
                log.i("test","_id="+_id+" address="+address+" body="+body+" date="+date+" type="+type);
                //通知适配器发生改变
                simpleadapter.notifydatasetchanged();
            }

        }
    }
}

最后需要在清单文件上配置读取短信的权限即可(androidmanifest.xml)

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。