萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> android從SIM卡中獲取聯系人方法

android從SIM卡中獲取聯系人方法

歡迎大家在這裡學習android從SIM卡中獲取聯系人!下面是我們給大家整理出來的精彩內容。希望大家在這裡學習!

privatestaticfinalintNAME_COLUMN=0;

  privatestaticfinalintNUMBER_COLUMN=1;

  privatestaticfinalStringSURI="content://icc/adn";

  privateContextcontext=null;

  privateContentResolverresolver=null;

  privateTelephonyManagertelMgr=null;

  publicSimHelper(Contextcontext)

  {

  this.context=context;

  resolver=context.getContentResolver();

  }

  publicTelephonyManagergetTelephonyManager()

  {

  if(telMgr==null)

  {

  telMgr=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

  }

  returntelMgr;

  }

  publicintgetSimCardState()

  {

  returngetTelephonyManager().getSimState();

  }

  publicList<ContactInfo>retrieveContactInfoFromSIM()

  {

  List<ContactInfo>simContactInfoList=newArrayList<ContactInfo>();

  Uriuri=Uri.parse(SURI);

  Cursorcursor=resolver.query(uri,null,null,null,null);

  for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext())

  {

  ContactInfocontacInfo=newContactInfo();

  finalNamePhoneTypePairnamePhoneTypePair=newNamePhoneTypePair(

  cursor.getString(NAME_COLUMN));

  finalStringname=namePhoneTypePair.name;

  finalintphoneType=namePhoneTypePair.phoneType;

  finalStringphoneNumber=cursor.getString(NUMBER_COLUMN);

  setPhoneInfo(contacInfo,phoneType,phoneNumber);

  contacInfo.setName(name);

  simContactInfoList.add(contacInfo);

  }

  cursor.close();

  returnsimContactInfoList;

  }

  privatevoidsetPhoneInfo(ContactInfocontactInfo,intphoneType,

  StringphoneNumber)

  {

  Phonephone=newPhone();

  phone.setType(PhoneType.valueOf(phoneType));

  phone.setNumber(phoneNumber);

  contactInfo.getPhones().add(phone);

  }

  privatestaticclassNamePhoneTypePair

  {

  finalStringname;

  finalintphoneType;

  publicNamePhoneTypePair(StringnameWithPhoneType)

  {

  //Lookfor/W/H/Mor/Oattheendofthenamesignifyingthetype

  intnameLen=nameWithPhoneType.length();

  if(nameLen-2>=0

  &&nameWithPhoneType.charAt(nameLen-2)=='/')

  {

  charc=Character.toUpperCase(nameWithPhoneType.charAt(nameLen-1));

  if(c=='W')

  {

  phoneType=PhoneType.Company.getPhoneType();

  }

  elseif(c=='M'||c=='O')

  {

  phoneType=PhoneType.Mobile.getPhoneType();

  }

  elseif(c=='H')

  {

  phoneType=PhoneType.Home.getPhoneType();

  }

  else

  {

  phoneType=PhoneType.Other.getPhoneType();

  }

  name=nameWithPhoneType.substring(0,nameLen-2);

  }

  else

  {

  phoneType=PhoneType.Other.getPhoneType();

  name=nameWithPhoneType;

  }

  }

  }

好了,android從SIM卡中獲取聯系人內容就給大家介紹到這裡了。希望大家繼續關注我們的網站!

相關推薦:

android接收系統自帶的廣播的方法

copyright © 萬盛學電腦網 all rights reserved