获取联系人效果图:
获取短信效果图:
前提--——添加权限:
通过短信数据库获取联系人内容:
Activity代码:
package com.example.android_readcontacts; 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.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity { private ContentResolver contentResolver; private List
layout代码:
xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 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" tools:context="com.example.android_readcontacts.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="getContacts" android:text="获取手机所有联系人" /> <ListView android:id="@+id/lt_main_listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="60dp"> ListView> RelativeLayout>
通过短信数据库获取短信内容:
Activity代码:
package com.example.android_readcontacts; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Created by Administrator on 2017/2/21. */ public class TwoActivity extends AppCompatActivity { private ContentResolver contentResolver; private List> data; private ListView lt_main_listview; private SimpleAdapter simpleAdapter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); lt_main_listview = (ListView) findViewById(R.id.lt_main_listview2); //获取内容监听者 contentResolver = getContentResolver(); //展示数据 data = new ArrayList >(); //准备适配器 simpleAdapter = new SimpleAdapter(this, data,android.R.layout.simple_list_item_2,new String[]{"number","body"},new int[]{android.R.id.text1,android.R.id.text2}); //设置适配器 lt_main_listview.setAdapter(simpleAdapter); } //获取系统短息地址 private Uri uri = Uri.parse("content://sms/"); //获取短信内容 public void getSms(View view){ //获取内容监听者 ContentResolver cr=getContentResolver(); //获取短信内容 String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"}; //创建查询 Cursor cursor=cr.query(uri,projection,null,null,"date desc"); if(null==cursor){ return; } while (cursor.moveToNext()){ Map map2=new HashMap<>(); //循环获取短信内容 //发件人地址 String number=cursor.getString(cursor.getColumnIndex("address")); map2.put("number",number); //发件人,如果发件人在通讯录中则为具体姓名,陌生人为null String name=cursor.getString(cursor.getColumnIndex("person")); map2.put("name",name); //短信具体内容 String body=cursor.getString(cursor.getColumnIndex("body")); map2.put("body",body); //获取自己短信服务号码中的验证码~~ Pattern pattern = Pattern.compile(" [a-zA-Z0-9]{10}"); Matcher matcher = pattern.matcher(body); if (matcher.find()) { String res = matcher.group().substring(1, 11); map2.put("content",res); } //放数据进集合 data.add(map2); } //通知适配器发生改变 simpleAdapter.notifyDataSetChanged(); } }
layout代码:
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="getSms" android:text="获取手机所有短信内容" /> <ListView android:id="@+id/lt_main_listview2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="60dp">ListView> LinearLayout>
SMS注释:
_id:短信序号,如100
thread_id:对话的序号,如100,与同一个手机号互发的短信,其序号是相同的
address:发件人地址,即手机号,如+8613811810000
person:发件人,如果发件人在通讯录中则为具体姓名,陌生人为null
date:日期,long型,如1256539465022,可以对日期显示格式进行设置
protocol:协议0SMS_RPOTO短信,1MMS_PROTO彩信
read:是否阅读0未读,1已读
status:短信状态-1接收,0complete,64pending,128failed
type:短信类型1是接收到的,2是已发出
body:短信具体内容
service_center:短信服务中心号码编号,如+8613800755500