Android -- Contentprovider---listview 实现调用通讯录和短信接收
2015-09-05 09:40
621 查看
获取权限
<uses-permission android:name="android.permission.READ_SMS"></uses-permission> <uses-permission android:name="android.permission.READ_CONTACTS"/><!-- 读取通讯录权限 --> <uses-permission android:name="android.permission.WRITE_CONTACTS"/><!-- 写入通讯录权限 -->
注册
<activity android:name=".MessageActivity"> </activity> <activity android:name=".NumberActivity" > </activity>
NumberActivty
public class NumberActivity extends Activity { ListView m_lstView; // 列表 private static final String NAME = "name"; private static final String NUMBER = "number"; List<HashMap<String,Object>> data; HashMap<String,Object>map; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.layout_listview); m_lstView = (ListView)findViewById(R.id.listViewNumber); data = new ArrayList<HashMap<String,Object>>(); getContacts(); ListAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_expandable_list_item_2, new String[]{"name","phone"}, new int[]{android.R.id.text1,android.R.id.text2} ); m_lstView.setAdapter(adapter); } private void getContacts() { // 得到ContentResolver对象 ContentResolver cr = this.getContentResolver(); // 取得电话本中开始一项的光标,主要就是查询"contacts"表 Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { map = new HashMap<String,Object>(); // 取得联系人名字 (显示出来的名字),实际内容在 ContactsContract.Contacts中 int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); String name = cursor.getString(nameIndex); map.put("name",name); // 取得联系人ID String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); // 根据联系人ID查询对应的电话号码 Cursor phoneNumbers = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); // 取得电话号码(可能存在多个号码) while (phoneNumbers.moveToNext()) { String strPhoneNumber = phoneNumbers.getString(phoneNumbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); map.put("phone", strPhoneNumber); } phoneNumbers.close(); // 根据联系人ID查询对应的email Cursor emails = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); // 取得email(可能存在多个email) while (emails.moveToNext()) { String strEmail = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); } emails.close(); data.add(map); } cursor.close(); } }
MessageActicty
public class MessageActivity extends Activity { final String SMS_URI_ALL = "content://sms/draft"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.layout_message); fillListView(); } private void fillListView() { ListView view = (ListView) findViewById(R.id.sms_list); ArrayList<HashMap<String, String>> list = readAllSMS(); SimpleAdapter listItemAdapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2, new String[] {"addr","body"}, new int[] {android.R.id.text1, android.R.id.text2} ); view.setAdapter(listItemAdapter); } private ArrayList<HashMap<String, String>> readAllSMS() { ContentResolver cr = getContentResolver(); String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" }; Uri uri = Uri.parse(SMS_URI_ALL); ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>> (); try{ Cursor cursor = cr.query(uri, projection, null, null, "date desc"); if(cursor.moveToFirst()) { int addrIdx = cursor.getColumnIndex("address"); int personIdx = cursor.getColumnIndex("person"); int bodyIdx = cursor.getColumnIndex("body"); do { String addr = cursor.getString(addrIdx); String person = cursor.getString(personIdx); String body = cursor.getString(bodyIdx); HashMap<String, String> item = new HashMap<String, String>(); item.put("addr", addr); item.put("person", person); item.put("body", body); list.add(item); Log.i("xiaoxiong", person); } while(cursor.moveToNext()); } return list; }catch (Exception ex) { Log.d("SQLiteException in getSmsInPhone", ex.getMessage()); } return list; } }
相关文章推荐
- android开发中如何获得应用的数字签名
- ANDROID开发之SQLite详解
- Android Studio如何快速生成get,set,tostring,构造函数
- Android基础View 绘制的流程
- Android中activity间数据传递方式
- Android 同一Activity 静态加载fragment 并实现通信
- android菜鸟进阶之路——线程的基本探究
- phonegap android插件,启动activity并返回值
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
- Android Studio 中快速提取方法
- Android Studio之Gradle多渠道打包
- Android 测试BOOT_COMPLETED 广播
- Android 低版本实现Tint--着色功能
- Android Source:优雅的repo sync自动重试
- android 自定义view(三)
- Android开发工具:Android Studio中Gradle的配置
- 第一个opencv4android项目
- Android Studio——Activity切换
- Android 编程下两种方式注册广播的区别
- Android截屏方法汇总