android利用ContentResolver访问者获取手机短信信息

利用ContentResolver访问者获取手机短信信息,在此记录一下,一遍以后查询。

首先看一下结果,结果如下:

android利用ContentResolver访问者获取手机短信信息_第1张图片

activity_message.xml类:

 
 
 

activity_xs.xml类




 

MessageActivity类:

public class MessageActivity extends AppCompatActivity {

 private ListView lv_message;
 private ContentResolver cr;
 private ArrayList> datalistView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_message);
  //获得短信的ID
  lv_message = (ListView) findViewById(R.id.lv_message);
  //得到访问者ContentResolver
  cr = getContentResolver();
  //定义一个接收短信的集合
  datalistView = new ArrayList<>();
  Uri uri = Uri.parse("content://sms/");
  Cursor cursor = cr.query(uri, null, null, null, null);
  while (cursor.moveToNext()) {
   String body = cursor.getString(cursor.getColumnIndex("body"));
   int address = cursor.getInt(cursor.getColumnIndex("address"));
   //将号码和短信内容放入Map集合中
   Map map = new HashMap<>();
   map.put("images", address+"");
   map.put("titles", body);
   datalistView.add(map);
  }
  SimpleAdapter adapter = new SimpleAdapter(this, datalistView, R.layout.activity_xs, new String[]{"images", "titles"}, new int[]{R.id.tv_name, R.id.tv_telephone});
  lv_message.setAdapter(adapter);
 }
}

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

你可能感兴趣的:(android利用ContentResolver访问者获取手机短信信息)