中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

android讀取sim聯(lián)系人資料的代碼

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
在獲取sim卡聯(lián)系人前,我們一般會先判斷sim卡狀態(tài),找到sim卡后再獲取它的資料,如下代碼我們可以讀取sim卡中的聯(lián)系人的一些信息。

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 + "讀數(shù)據\n";  
            return ;  
        }  
        mString += "第一列:" + mCursor.getColumnName(0) + "\n";  
        mString += "第二列:" + mCursor.getColumnName(1) + "\n";  
        mString += "第三列:" + mCursor.getColumnName(2) + "\n";  
        mString += "第四列:" + mCursor.getColumnName(3) + "\n";  
        mString += "列數(shù):" + mCursor.getColumnCount() + "\n";  
        mString += "行數(shù):" + mCursor.getCount() + "\n";  
        if (mCursor != null) {  
            while (mCursor.moveToNext()) {  
                // 取得聯(lián)系人名字  
                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 = "未知狀態(tài)";  
                 // 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> 

標簽: 代碼

版權申明:本站文章部分自網絡,如有侵權,請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:iOS對圖像進行壓縮代碼

下一篇:iOS檢查網絡是否可用的代碼