[Android教程] android读取sim联系人资料的代码

来源:互联网 发布:腾讯云数据库怎么连接 编辑:程序博客网 时间:2024/05/29 14:27
在获取sim卡联系人前,我们一般会先判断sim卡状态,找到sim卡后再获取它的资料,如下代码我们可以读取sim卡中的联系人的一些信息。

PhoneTest.java

  1. package com.android.test;
  2. http://www.kmrlyy.com/gjjb/33581.html
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.database.Cursor;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.telephony.TelephonyManager;
  10. import android.widget.TextView;
  11. public class PhoneTest extends Activity {
  12. private TextView mTextView;
  13. protected Cursor mCursor = null;
  14. private TelephonyManager mTelephonyManager;
  15. private String mString = "";
  16. /** Called when the activity is first created. */
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. mTextView = (TextView)findViewById(R.id.text);
  22. mTextView.setTextSize(20.3f);
  23. isSimExist();
  24. if(getSimState() == TelephonyManager.SIM_STATE_READY){
  25. mString += " 卡存在\n";
  26. getSimContacts("content://icc/adn"); //一般用这一条,如果这条不行的话可以试试下面的一条。
  27. getSimContacts("content://sim/adn");//此种读法在我们手机里不能读取,所以,还是用上个uri比较好。
  28. }
  29. mTextView.setText(mString);
  30. }
  31. http://www.kmrlyy.com/gongjingxirou/33582.html
  32. private void getSimContacts(String str){
  33. Intent intent = new Intent();
  34. intent.setData(Uri.parse(str));
  35. Uri uri = intent.getData();
  36. mCursor = getContentResolver().query(uri, null, null, null, null);
  37. if(mCursor == null){
  38. mString += "不能从" + str + "读数据\n";
  39. return ;
  40. }
  41. mString += "第一列:" + mCursor.getColumnName(0) + "\n";
  42. mString += "第二列:" + mCursor.getColumnName(1) + "\n";
  43. mString += "第三列:" + mCursor.getColumnName(2) + "\n";
  44. mString += "第四列:" + mCursor.getColumnName(3) + "\n";
  45. mString += "列数:" + mCursor.getColumnCount() + "\n";
  46. mString += "行数:" + mCursor.getCount() + "\n";
  47. if (mCursor != null) {
  48. while (mCursor.moveToNext()) {
  49. // 取得联系人名字
  50. int nameFieldColumnIndex = mCursor.getColumnIndex("name");
  51. mString += mCursor.getString(nameFieldColumnIndex)+" ";
  52. // 取得电话号码
  53. int numberFieldColumnIndex = mCursor
  54. .getColumnIndex("number");
  55. mString += mCursor.getString(numberFieldColumnIndex)+" ";
  56. // 取得邮箱
  57. int emailsFieldColumnIndex = mCursor
  58. .getColumnIndex("emails");
  59. mString += mCursor.getString(emailsFieldColumnIndex)+" ";
  60. // 取得id
  61. int idFieldColumnIndex = mCursor
  62. .getColumnIndex("_id");
  63. mString += mCursor.getString(idFieldColumnIndex)+"\n";
  64. }
  65. }
  66. mString += mCursor + "\n";
  67. mCursor.close();
  68. }
  69. private int getSimState(){
  70. return mTelephonyManager.getSimState();
  71. }
  72. private void isSimExist(){
  73. mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  74. int simState = mTelephonyManager.getSimState();
  75. switch (simState) {
  76. case TelephonyManager.SIM_STATE_ABSENT:
  77. mString = "无卡";
  78. // do something
  79. break;
  80. case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
  81. mString = "需要NetworkPIN解锁";
  82. // do something
  83. http://www.kmrlyy.com/gongjingxirou/33583.html
  84. break;
  85. case TelephonyManager.SIM_STATE_PIN_REQUIRED:
  86. mString = "需要PIN解锁";
  87. // do something
  88. break;
  89. case TelephonyManager.SIM_STATE_PUK_REQUIRED:
  90. mString = "需要PUN解锁";
  91. // do something
  92. break;
  93. case TelephonyManager.SIM_STATE_READY:
  94. mString = "良好";
  95. // do something
  96. break;
  97. case TelephonyManager.SIM_STATE_UNKNOWN:
  98. mString = "未知状态";
  99. // do something
  100. break;
  101. }
  102. mTextView.setText(mString);
  103. }
  104. }
复制代码

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <ScrollView android:layout_width="fill_parent"
  8. android:layout_height="fill_parent">
  9. <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>
复制代码

AndroidManefist.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.android.test"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. http://www.kmrlyy.com/gongwaiyun/33585.html
  7. <application android:icon="@drawable/icon" android:label="@string/app_name">
  8. <activity android:name=".PhoneTest"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12. <category android:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>www.kmrlyy.com
  15. http://www.kmrlyy.com/gongjingyan/33584.html
  16. </application>
  17. <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
  18. </manifest>
0 0