在写程序中,有时候可能需要获取sim卡中的一些联系人资料。在获取sim卡联系人前,我们一般会先判断sim卡状态,找到sim卡后再获取它的资料,如下代码我们可以读取sim卡中的联系人的一些信息。
读取的代码如下:
package com.android.test; |
import android.app.Activity; |
import android.content.Context; |
import android.content.Intent; |
import android.database.Cursor; |
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. */ |
public void onCreate(Bundle savedInstanceState) { |
super.onCreate(savedInstanceState); |
setContentView(R.layout.main); |
mTextView = (TextView)findViewById(R.id.text); |
mTextView.setTextSize(20.3f); |
if(getSimState() == TelephonyManager.SIM_STATE_READY){ |
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); |
mString += "不能从" + str + "读数据\n"; |
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"; |
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)+" "; |
int idFieldColumnIndex = mCursor |
mString += mCursor.getString(idFieldColumnIndex)+"\n"; |
mString += mCursor + "\n"; |
private int getSimState(){ |
return mTelephonyManager.getSimState(); |
private void isSimExist(){ |
mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); |
int simState = mTelephonyManager.getSimState(); |
case TelephonyManager.SIM_STATE_ABSENT: |
case TelephonyManager.SIM_STATE_NETWORK_LOCKED: |
mString = "需要NetworkPIN解锁"; |
case TelephonyManager.SIM_STATE_PIN_REQUIRED: |
case TelephonyManager.SIM_STATE_PUK_REQUIRED: |
case TelephonyManager.SIM_STATE_READY: |
case TelephonyManager.SIM_STATE_UNKNOWN: |
mTextView.setText(mString); |
main.xml布局文件如下:
<?xml version="1.0" encoding="utf-8"?> |
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" |
同时还要在AndroidManefist.xml中添加读sim卡的权限,如下:
<?xml version="1.0" encoding="utf-8"?> |
package="com.android.test" |
android:versionName="1.0"> |
<application android:icon="@drawable/icon" android:label="@string/app_name"> |
<activity android:name=".PhoneTest" |
android:label="@string/app_name"> |
<action android:name="android.intent.action.MAIN" /> |
<category android:name="android.intent.category.LAUNCHER" /> |
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission> |
以后一定会用到。。