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); |
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" |
3. [代碼]AndroidManefist.xml
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 > |
本文由用戶
jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!