android中判断sim卡状态和读取联系人资料的方法

jopen 11年前

在写程序中,有时候可能需要获取sim卡中的一些联系人资料。在获取sim卡联系人前,我们一般会先判断sim卡状态,找到sim卡后再获取它的资料,如下代码我们可以读取sim卡中的联系人的一些信息。

1. [代码]PhoneTest.java     

001 package com.android.test; 
002     
003 import android.app.Activity; 
004 import android.content.Context; 
005 import android.content.Intent; 
006 import android.database.Cursor; 
007 import android.net.Uri; 
008 import android.os.Bundle; 
009 import android.telephony.TelephonyManager; 
010 import android.widget.TextView; 
011     
012 public class PhoneTest extends Activity { 
013     private TextView mTextView; 
014     protected Cursor mCursor = null
015     private TelephonyManager mTelephonyManager; 
016     private String mString = ""
017         
018     /** Called when the activity is first created. */
019     @Override
020     public void onCreate(Bundle savedInstanceState) { 
021         super.onCreate(savedInstanceState); 
022         setContentView(R.layout.main); 
023         mTextView = (TextView)findViewById(R.id.text); 
024         mTextView.setTextSize(20.3f); 
025         isSimExist(); 
026         if(getSimState() == TelephonyManager.SIM_STATE_READY){ 
027             mString += "    卡存在\n"
028             getSimContacts("content://icc/adn"); //一般用这一条,如果这条不行的话可以试试下面的一条。 
029             getSimContacts("content://sim/adn");//此种读法在我们手机里不能读取,所以,还是用上个uri比较好。 
030         
031         mTextView.setText(mString); 
032     
033         
034     private void getSimContacts(String str){ 
035         Intent intent = new Intent(); 
036         intent.setData(Uri.parse(str)); 
037         Uri uri = intent.getData(); 
038         mCursor = getContentResolver().query(uri, nullnullnullnull); 
039         if(mCursor == null){ 
040             mString += "不能从" + str + "读数据\n"
041             return 
042         
043         mString += "第一列:" + mCursor.getColumnName(0) + "\n"
044         mString += "第二列:" + mCursor.getColumnName(1) + "\n"
045         mString += "第三列:" + mCursor.getColumnName(2) + "\n"
046         mString += "第四列:" + mCursor.getColumnName(3) + "\n"
047         mString += "列数:" + mCursor.getColumnCount() + "\n"
048         mString += "行数:" + mCursor.getCount() + "\n"
049         if (mCursor != null) { 
050             while (mCursor.moveToNext()) { 
051                 // 取得联系人名字 
052                 int nameFieldColumnIndex = mCursor.getColumnIndex("name"); 
053                 mString += mCursor.getString(nameFieldColumnIndex)+"    "
054                 // 取得电话号码 
055                 int numberFieldColumnIndex = mCursor 
056                         .getColumnIndex("number"); 
057                 mString += mCursor.getString(numberFieldColumnIndex)+"    "
058                 // 取得邮箱 
059                 int emailsFieldColumnIndex = mCursor 
060                         .getColumnIndex("emails"); 
061                 mString += mCursor.getString(emailsFieldColumnIndex)+"    "
062                 // 取得id 
063                 int idFieldColumnIndex = mCursor 
064                         .getColumnIndex("_id"); 
065                 mString += mCursor.getString(idFieldColumnIndex)+"\n"
066             
067         
068         mString += mCursor + "\n"
069         mCursor.close(); 
070     
071         
072     private int getSimState(){ 
073         return mTelephonyManager.getSimState(); 
074     
075         
076     private void isSimExist(){ 
077         mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
078          int simState = mTelephonyManager.getSimState(); 
079     
080          switch (simState) { 
081     
082              case TelephonyManager.SIM_STATE_ABSENT: 
083                  mString = "无卡"
084                  // do something 
085                  break
086     
087              case TelephonyManager.SIM_STATE_NETWORK_LOCKED: 
088                  mString = "需要NetworkPIN解锁"
089                  // do something 
090     
091                  break
092     
093              case TelephonyManager.SIM_STATE_PIN_REQUIRED: 
094                  mString = "需要PIN解锁"
095                  // do something 
096                  break
097     
098              case TelephonyManager.SIM_STATE_PUK_REQUIRED: 
099                  mString = "需要PUN解锁"
100                  // do something 
101                  break
102     
103              case TelephonyManager.SIM_STATE_READY: 
104                  mString = "良好"
105                  // do something 
106                  break
107     
108              case TelephonyManager.SIM_STATE_UNKNOWN: 
109                  mString = "未知状态"
110                  // do something 
111                  break
112          
113          mTextView.setText(mString); 
114     
115         
116 }
</div> </div> </div>

2. [代码]main.xml     

01 <?xml version="1.0" encoding="utf-8"?> 
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:orientation="vertical"
04     android:layout_width="fill_parent"
05     android:layout_height="fill_parent"
06     
07     <ScrollView android:layout_width="fill_parent"
08     android:layout_height="fill_parent"
09     <LinearLayout android:orientation="vertical"
10     android:layout_width="fill_parent"
11     android:layout_height="fill_parent"
12     <TextView  android:id="@+id/text"
13     android:layout_width="fill_parent" 
14     android:layout_height="wrap_content" 
15     android:text="@string/hello"
16     /> 
17     </LinearLayout
18     </ScrollView
19 </LinearLayout>
</div> </div> </div>

3. [代码]AndroidManefist.xml     

</div>
01 <?xml version="1.0" encoding="utf-8"?> 
02 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
03       package="com.android.test"
04       android:versionCode="1"
05       android:versionName="1.0"
06     
07     
08     <application android:icon="@drawable/icon" android:label="@string/app_name"
09         <activity android:name=".PhoneTest"
10                   android:label="@string/app_name"
11             <intent-filter
12                 <action android:name="android.intent.action.MAIN" /> 
13                 <category android:name="android.intent.category.LAUNCHER" /> 
14             </intent-filter
15         </activity
16     
17     </application
18     <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission
19 </manifest>