package com.example.administrator.android_jiewen;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
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;
public class MainActivity extends AppCompatActivity {
private ContentResolver cr;
private ListView lt_main_id;
private List<Map<String, Object>> date;
private SimpleAdapter simpleAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取内容访问者
cr = getContentResolver();
lt_main_id =(ListView) findViewById(R.id.lt_main_id);
date = new ArrayList<Map<String, Object>>();
//适配器
simpleAdapter = new SimpleAdapter(this,date,android.R.layout.simple_list_item_2,new String[]{"names","phones"},new int[]{android.R.id.text1,android.R.id.text2});
lt_main_id.setAdapter(simpleAdapter);
}
public void getDate(View view) {
//获取联系人的id和姓名
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
Cursor Cursor = cr.query(uri, null, null, null, null);
while (Cursor.moveToNext()) {
Map<String, Object> map = new HashMap<String, Object>();
int n = Cursor.getInt(Cursor.getColumnIndex("_id"));
String name = Cursor.getString(Cursor.getColumnIndex("display_name"));
// String address=Cursor.getString(Cursor.getColumnIndex("address"));
Log.i("test", n + "" + name);
map.put("names", name);
// map.put("names",address);
Uri uri2 = Uri.parse("content://com.android.contacts/raw_contacts/"+n+"/data");
Cursor Cursor2 = cr.query(uri2, null, null, null, null);
while (Cursor2.moveToNext()) {
//int n2= Cursor2.getInt(Cursor2.getColumnIndex("mimetype_id"));
// String data1 = Cursor2.getString(Cursor2.getColumnIndex("data1"));
String s2 = Cursor2.getString(Cursor2.getColumnIndex("mimetype"));
String data1=null;
if("vnd.android.cursor.item/phone_v2".equals(s2)){
Log.i("test","fff");
data1=Cursor2.getString(Cursor2.getColumnIndex("data1"));
Log.i("test", data1 + "" + s2);
map.put("phones", data1);
}
}
date.add(map);
}
//通知适配器发生改变
simpleAdapter.notifyDataSetChanged();;
}
}
图片如下:
获取电话号码
获取短信内容
package com.example.administrator.android_jiewen;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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;
/**
* Created by Administrator on 2017/2/21 0021.
*/
public class MainActivity02 extends AppCompatActivity {
private ContentResolver cr;
private ListView lv_main_id;
private List<Map<String, Object>> data;
private SimpleAdapter simpleAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main02);
cr = getContentResolver();
lv_main_id = (ListView) findViewById(R.id.lv_main_id);
data = new ArrayList<Map<String, Object>>();
simpleAdapter = new SimpleAdapter(this,data,android.R.layout.simple_list_item_2,new String[]{"names","phones"},new int[]{android.R.id.text1,android.R.id.text2});
lv_main_id.setAdapter(simpleAdapter);
cr=getContentResolver();
}
public void getDate(View view){
Uri uri= Uri.parse("content://sms/");
Cursor Cursor= cr.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);
simpleAdapter.notifyDataSetChanged();
}
}
}
}
短信图片显示
![]()
获取电话号码和短信权限
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>