欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android利用ContentProvider读取短信内容

时间:2023-02-05 09:39:20|栏目:Android代码|点击:

本文实例为大家分享了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" />

上一篇:Android通过SeekBar调节布局背景颜色

栏    目:Android代码

下一篇:实例讲解Android Fragment的两种使用方法

本文标题:Android利用ContentProvider读取短信内容

本文地址:http://www.codeinn.net/misctech/225118.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有