Android(安卓)Contacts(一)—— 读取联系人
Android Contacts(一)—— 读取联系人
分类:Android 2012-03-07 14:01 3509人阅读 评论(0) 收藏 举报 android listview string sqlite list nullIntroduction To Android Contacts
Learn to work with the Android contacts database. Basic knowledge of accessing SQLite in Android along with using Cursors is expected. See theAndroid SQLite and Cursor Articlefor more information. Google changed the contacts database moving from 1.x to 2.0 versions of Android. This tutorial will be broken into 3 sections. First covering accessing contacts in Android 2.0. The second page will deal with accessing the contacts in Android 1.6 and before. Third we'll glue it all together with a class that abstracts specific classes for each version and a set of classes to manage the data from the contact records.
Contacts 读取代码:
[java] view plain copy print ?
- packagecom.homer.phone;
- importjava.util.ArrayList;
- importjava.util.HashMap;
- importandroid.app.Activity;
- importandroid.database.Cursor;
- importandroid.os.Bundle;
- importandroid.provider.ContactsContract;
- importandroid.provider.ContactsContract.CommonDataKinds.Phone;
- importandroid.widget.ListView;
- importandroid.widget.SimpleAdapter;
- publicclassphoneReadextendsActivity{
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- showListView();
- }
- privatevoidshowListView(){
- ListViewlistView=newListView(this);
- ArrayList<HashMap<String,String>>list=getPeopleInPhone2();
- SimpleAdapteradapter=newSimpleAdapter(
- this,
- list,
- android.R.layout.simple_list_item_2,
- newString[]{"peopleName","phoneNum"},
- newint[]{android.R.id.text1,android.R.id.text2}
- );
- listView.setAdapter(adapter);
- setContentView(listView);
- }
- privateArrayList<HashMap<String,String>>getPeopleInPhone2(){
- ArrayList<HashMap<String,String>>list=newArrayList<HashMap<String,String>>();
- Cursorcursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);//获取手机联系人
- while(cursor.moveToNext()){
- HashMap<String,String>map=newHashMap<String,String>();
- intindexPeopleName=cursor.getColumnIndex(Phone.DISPLAY_NAME);//peoplename
- intindexPhoneNum=cursor.getColumnIndex(Phone.NUMBER);//phonenumber
- StringstrPeopleName=cursor.getString(indexPeopleName);
- StringstrPhoneNum=cursor.getString(indexPhoneNum);
- map.put("peopleName",strPeopleName);
- map.put("phoneNum",strPhoneNum);
- list.add(map);
- }
- if(!cursor.isClosed()){
- cursor.close();
- cursor=null;
- }
- returnlist;
- }
- }
AndroidManifest.xml 权限
记得在AndroidManifest.xml中加入android.permission.READ_CONTACTS这个permission
<uses-permission android:name="android.permission.READ_CONTACTS" />
运行结果:
代码示例
Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联
分类:Android 2012-03-07 14:23 4019人阅读 评论(3) 收藏 举报 sms android string null date 手机Android 的SMS读取短信,可以获取发信人/收信人的手机号码(address),Contacts的联系人,可以过滤手机号码(address),因此SMS可以通过手机号码(address)关联到Contacts联系人
SMS - Contacts 关联代码
[java] view plain copy print ?
- //通过address手机号关联Contacts联系人的显示名字
- privateStringgetPeopleNameFromPerson(Stringaddress){
- if(address==null||address==""){
- return"(noaddress)\n";
- }
- StringstrPerson="null";
- String[]projection=newString[]{Phone.DISPLAY_NAME,Phone.NUMBER};
- Uriuri_Person=Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,address);//address手机号过滤
- Cursorcursor=getContentResolver().query(uri_Person,projection,null,null,null);
- if(cursor.moveToFirst()){
- intindex_PeopleName=cursor.getColumnIndex(Phone.DISPLAY_NAME);
- StringstrPeopleName=cursor.getString(index_PeopleName);
- strPerson=strPeopleName;
- }
- cursor.close();
- returnstrPerson;
- }
[java] view plain copy print ?
- packagecom.homer.phone;
- importjava.sql.Date;
- importjava.text.SimpleDateFormat;
- importandroid.app.Activity;
- importandroid.database.Cursor;
- importandroid.database.sqlite.SQLiteException;
- importandroid.net.Uri;
- importandroid.os.Bundle;
- importandroid.provider.ContactsContract;
- importandroid.provider.ContactsContract.CommonDataKinds.Phone;
- importandroid.util.Log;
- importandroid.widget.ScrollView;
- importandroid.widget.TextView;
- publicclassphoneRead2extendsActivity{
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- TextViewtv=newTextView(this);
- tv.setText(getSmsInPhone());
- ScrollViewsv=newScrollView(this);
- sv.addView(tv);
- setContentView(sv);
- }
- publicStringgetSmsInPhone(){
- finalStringSMS_URI_ALL="content://sms/";
- finalStringSMS_URI_INBOX="content://sms/inbox";
- finalStringSMS_URI_SEND="content://sms/sent";
- finalStringSMS_URI_DRAFT="content://sms/draft";
- finalStringSMS_URI_OUTBOX="content://sms/outbox";
- finalStringSMS_URI_FAILED="content://sms/failed";
- finalStringSMS_URI_QUEUED="content://sms/queued";
- StringBuildersmsBuilder=newStringBuilder();
- try{
- Uriuri=Uri.parse(SMS_URI_ALL);
- String[]projection=newString[]{"_id","address","person","body","date","type"};
- Cursorcur=getContentResolver().query(uri,projection,null,null,"datedesc");//获取手机内部短信
- if(cur.moveToFirst()){
- intindex_Address=cur.getColumnIndex("address");
- intindex_Person=cur.getColumnIndex("person");
- intindex_Body=cur.getColumnIndex("body");
- intindex_Date=cur.getColumnIndex("date");
- intindex_Type=cur.getColumnIndex("type");
- do{
- StringstrAddress=cur.getString(index_Address);
- intintPerson=cur.getInt(index_Person);
- Stringstrbody=cur.getString(index_Body);
- longlongDate=cur.getLong(index_Date);
- intintType=cur.getInt(index_Type);
- SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss");
- Dated=newDate(longDate);
- StringstrDate=dateFormat.format(d);
- StringstrType="";
- if(intType==1){
- strType="接收";
- }elseif(intType==2){
- strType="发送";
- }else{
- strType="null";
- }
- StringstrAddress2=getPeopleNameFromPerson(strAddress);
- smsBuilder.append("[");
- //smsBuilder.append(strAddress+",");
- smsBuilder.append(strAddress+":"+strAddress2+",");
- smsBuilder.append(intPerson+",");
- smsBuilder.append(strbody+",");
- smsBuilder.append(strDate+",");
- smsBuilder.append(strType);
- smsBuilder.append("]\n\n");
- }while(cur.moveToNext());
- if(!cur.isClosed()){
- cur.close();
- cur=null;
- }
- }else{
- smsBuilder.append("noresult!");
- }//endif
- smsBuilder.append("getSmsInPhonehasexecuted!");
- }catch(SQLiteExceptionex){
- Log.d("SQLiteExceptioningetSmsInPhone",ex.getMessage());
- }
- returnsmsBuilder.toString();
- }
- //通过address手机号关联Contacts联系人的显示名字
- privateStringgetPeopleNameFromPerson(Stringaddress){
- if(address==null||address==""){
- return"(noaddress)\n";
- }
- StringstrPerson="null";
- String[]projection=newString[]{Phone.DISPLAY_NAME,Phone.NUMBER};
- Uriuri_Person=Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,address);//address手机号过滤
- Cursorcursor=getContentResolver().query(uri_Person,projection,null,null,null);
- if(cursor.moveToFirst()){
- intindex_PeopleName=cursor.getColumnIndex(Phone.DISPLAY_NAME);
- StringstrPeopleName=cursor.getString(index_PeopleName);
- strPerson=strPeopleName;
- }
- cursor.close();
- returnstrPerson;
- }
- }
AndroidManifest.xml 权限
记得在AndroidManifest.xml中加入android.permission.READ_SMS和android.permission.READ_CONTACTS这两个permission
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
示例代码
更多相关文章
- FragmentTabhost记录
- android 短信验证自动获取验证码
- Android获取本机电话号码的简单方法
- 获取Android的Java源代码并在Eclipse中关联查看的最新方法
- Android联系人数据库全
- Android(安卓)SMS(一) —— 读取短信
- Android(安卓)发送短信 sms
- android获取手机中的短信和,android获取手机通讯录联系人和号码
- Android之使用ContentResolver对通信录中的数据进行简单操作