首页 > 代码库 > android中判断sim卡状态和读取联系人资料的方法
android中判断sim卡状态和读取联系人资料的方法
在写程序中,有时候可能需要获取sim卡中的一些联系人资料。在获取sim卡联系人前,我们一般会先判断sim卡状态,找到sim卡后再获取它的资料,如下代码我们可以读取sim卡中的联系人的一些信息。
PhoneTest.java package com.android.test; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.telephony.TelephonyManager; import android.widget.TextView; public class PhoneTest extends Activity { private TextView mTextView; protected Cursor mCursor = null; private TelephonyManager mTelephonyManager; private String mString = ""; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView = (TextView)findViewById(R.id.text); mTextView.setTextSize(20.3f); isSimExist(); if(getSimState() == TelephonyManager.SIM_STATE_READY){ mString += " 卡存在\n"; getSimContacts("content://icc/adn"); //一般用这一条,如果这条不行的话可以试试下面的一条。 getSimContacts("content://sim/adn");//此种读法在我们手机里不能读取,所以,还是用上个uri比较好。 } mTextView.setText(mString); } private void getSimContacts(String str){ Intent intent = new Intent(); intent.setData(Uri.parse(str)); Uri uri = intent.getData(); mCursor = getContentResolver().query(uri, null, null, null, null); if(mCursor == null){ mString += "不能从" + str + "读数据\n"; return ; } mString += "第一列:" + mCursor.getColumnName(0) + "\n"; mString += "第二列:" + mCursor.getColumnName(1) + "\n"; mString += "第三列:" + mCursor.getColumnName(2) + "\n"; mString += "第四列:" + mCursor.getColumnName(3) + "\n"; mString += "列数:" + mCursor.getColumnCount() + "\n"; mString += "行数:" + mCursor.getCount() + "\n"; if (mCursor != null) { while (mCursor.moveToNext()) { // 取得联系人名字 int nameFieldColumnIndex = mCursor.getColumnIndex("name"); mString += mCursor.getString(nameFieldColumnIndex)+" "; // 取得电话号码 int numberFieldColumnIndex = mCursor .getColumnIndex("number"); mString += mCursor.getString(numberFieldColumnIndex)+" "; // 取得邮箱 int emailsFieldColumnIndex = mCursor .getColumnIndex("emails"); mString += mCursor.getString(emailsFieldColumnIndex)+" "; // 取得id int idFieldColumnIndex = mCursor .getColumnIndex("_id"); mString += mCursor.getString(idFieldColumnIndex)+"\n"; } } mString += mCursor + "\n"; mCursor.close(); } private int getSimState(){ return mTelephonyManager.getSimState(); } private void isSimExist(){ mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); int simState = mTelephonyManager.getSimState(); switch (simState) { case TelephonyManager.SIM_STATE_ABSENT: mString = "无卡"; // do something break; case TelephonyManager.SIM_STATE_NETWORK_LOCKED: mString = "需要NetworkPIN解锁"; // do something break; case TelephonyManager.SIM_STATE_PIN_REQUIRED: mString = "需要PIN解锁"; // do something break; case TelephonyManager.SIM_STATE_PUK_REQUIRED: mString = "需要PUN解锁"; // do something break; case TelephonyManager.SIM_STATE_READY: mString = "良好"; // do something break; case TelephonyManager.SIM_STATE_UNKNOWN: mString = "未知状态"; // do something break; } mTextView.setText(mString); } } [代码] main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> </ScrollView> </LinearLayout> [代码] AndroidManefist.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.test" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".PhoneTest" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission> </manifest>
<iframe id="google_ads_frame2" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1400246933&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao1234%2Fp%2F3727009.html&dt=1400246933685&shv=r20140513&cbv=r20140417&saldr=sb&correlator=1400246933531&frm=20&ga_vid=227879072.1400240514&ga_sid=1400240514&ga_hid=1700729136&ga_fc=1&u_tz=480&u_his=406&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=16&adx=0&ady=3360&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=2&xpc=XFdmv5OFIn&p=http%3A//www.cnblogs.com&dtd=50" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame2" marginWidth="0" scrolling="no" hspace="0"></iframe><iframe id="google_ads_frame3" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1400246933&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao1234%2Fp%2F3727009.html&dt=1400246933742&shv=r20140513&cbv=r20140417&saldr=sb&prev_slotnames=8660799060&correlator=1400246933531&frm=20&ga_vid=227879072.1400240514&ga_sid=1400240514&ga_hid=1700729136&ga_fc=1&u_tz=480&u_his=406&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=16&adx=306&ady=3610&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=3&xpc=Y6uURAEEGZ&p=http%3A//www.cnblogs.com&dtd=46" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame3" marginWidth="0" scrolling="no" hspace="0"></iframe>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。