android中判断sim卡状态和读取联系人资料的方法
在写程序中,有时候可能需要获取sim卡中的一些联系人资料。在获取sim卡联系人前,我们一般会先判断sim卡状态,找到sim卡后再获取它的资料,如下代码我们可以读取sim卡中的联系人的一些信息。
1. [代码]PhoneTest.java
001 | package com.android.test; |
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; |
012 | public class PhoneTest extends Activity { |
013 | private TextView mTextView; |
014 | protected Cursor mCursor = null; |
015 | private TelephonyManager mTelephonyManager; |
016 | private String mString = ""; |
018 | /** Called when the activity is first created. */ |
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); |
026 | if(getSimState() == TelephonyManager.SIM_STATE_READY){ |
028 | getSimContacts("content://icc/adn"); //一般用这一条,如果这条不行的话可以试试下面的一条。 |
029 | getSimContacts("content://sim/adn");//此种读法在我们手机里不能读取,所以,还是用上个uri比较好。 |
031 | mTextView.setText(mString); |
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, null, null, null, null); |
040 | mString += "不能从" + str + "读数据\n"; |
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()) { |
052 | int nameFieldColumnIndex = mCursor.getColumnIndex("name"); |
053 | mString += mCursor.getString(nameFieldColumnIndex)+" "; |
055 | int numberFieldColumnIndex = mCursor |
056 | .getColumnIndex("number"); |
057 | mString += mCursor.getString(numberFieldColumnIndex)+" "; |
059 | int emailsFieldColumnIndex = mCursor |
060 | .getColumnIndex("emails"); |
061 | mString += mCursor.getString(emailsFieldColumnIndex)+" "; |
063 | int idFieldColumnIndex = mCursor |
064 | .getColumnIndex("_id"); |
065 | mString += mCursor.getString(idFieldColumnIndex)+"\n"; |
068 | mString += mCursor + "\n"; |
072 | private int getSimState(){ |
073 | return mTelephonyManager.getSimState(); |
076 | private void isSimExist(){ |
077 | mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); |
078 | int simState = mTelephonyManager.getSimState(); |
082 | case TelephonyManager.SIM_STATE_ABSENT: |
087 | case TelephonyManager.SIM_STATE_NETWORK_LOCKED: |
088 | mString = "需要NetworkPIN解锁"; |
093 | case TelephonyManager.SIM_STATE_PIN_REQUIRED: |
098 | case TelephonyManager.SIM_STATE_PUK_REQUIRED: |
103 | case TelephonyManager.SIM_STATE_READY: |
108 | case TelephonyManager.SIM_STATE_UNKNOWN: |
113 | mTextView.setText(mString); |
</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" |
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" |
</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"> |
08 | <application android:icon="@drawable/icon" android:label="@string/app_name"> |
09 | <activity android:name=".PhoneTest" |
10 | android:label="@string/app_name"> |
12 | <action android:name="android.intent.action.MAIN" /> |
13 | <category android:name="android.intent.category.LAUNCHER" /> |
18 | <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission> |