十九、android中判断sim卡状态和读取联系人资料的方法
在写程序中,有时候可能需要获取sim卡中的一些联系人资料。在获取sim卡联系人前,我们一般会先判断sim卡状态,找到sim卡后再获取它的资料,如下代码我们可以读取sim卡中的联系人的一些信息。

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 + "读数据\n";
return ;
}
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";
if (mCursor != null) {
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)+" ";
// 取得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 = "未知状态";
// 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>

十九、android中判断sim卡状态和读取联系人资料的方法的更多相关文章
- android中判断sim卡状态和读取联系人资料的方法
在写程序中,有时候可能需要获取sim卡中的一些联系人资料.在获取sim卡联系人前,我们一般会先判断sim卡状态,找到sim卡后再获取它的资料,如下代码我们可以读取sim卡中的联系人的一些信息. Pho ...
- 017 Android 获取手机SIM卡序列号和读取联系人
1.获取手机SIM卡序列号 //5.存储sim卡系列号 //5.1获取sim卡系列号 TelephonyManager manager = (TelephonyManager) getSystemSe ...
- TelephonyManager类:Android手机及Sim卡状态的获取
TelephonyManager这个类很有用,可以得到很多关于手机和Sim卡的信息. 直接上注释后的代码,请享用 package net.sunniwell.app;import android.ap ...
- Android本机号码及Sim卡状态的获取
SIM卡存储的数据可分为四类:第一类是固定存放的数据.这类数据在移动电话机被出售之前由SIM卡中心写入,包括国际移动用户识别号(IMSI).鉴权密钥(KI).鉴权和加密算法等等.第二类是暂时存放的有关 ...
- Android ExpandableListView使用+获取SIM卡状态信息
ExpandableListView 是一个可以实现下拉列表的控件,大家可能都用过QQ,QQ中的好友列表就是用ExpandableListView实现的,不过它是自定义的适配器.本篇 博客除了要介绍E ...
- Android(java)学习笔记183:判断SD卡状态和存储空间大小
当我们在使用SD卡时候,如果我们想往SD卡里读写数据,我们必须在这之前进行一个逻辑判断,那就是判断SD卡状态和SD存储空间大小: 核心代码: String status = Environment.g ...
- Android记录3--ExpandableListView使用+获取SIM卡状态信息
Android记录3--ExpandableListView使用+获取SIM卡状态信息 2013年8月9日Android记录 ExpandableListView是一个可以实现下拉列表的控件,大家可能 ...
- Android(java)学习笔记126:判断SD卡状态和SD卡容量
1. 判断SD卡状态和SD卡存储空间大小 当我们在使用SD卡时候,如果我们想往SD卡里读写数据,我们必须在这之前进行一个逻辑判断,那就是判断SD卡状态和SD存储空间大小: 核心代码: String s ...
- Android中判断网络连接是否可用及监控网络状态
Android中判断网络连接是否可用及监控网络状态 作者: 字体:[增加 减小] 类型:转载 获取网络信息需要在AndroidManifest.xml文件中加入相应的权限,接下来详细介绍Android ...
随机推荐
- NuMicro Coretex™-M0家族中哪种芯片支持UID (Unique ID)? 用户该怎么做才能对其芯片进行加密功能?
http://www.nuvoton.com/hq/chs/productfaqs/Pages/00000001.aspx 是的,使用者可利用UID来对以下系列芯片进行加密, Mini51 Serie ...
- DCEF3 相关资料
DCEF3 调用 js http://www.cnblogs.com/Delphi-Farmer/p/4103708.html interface uses ceflib;//其它 type //这里 ...
- 博客标题栏增加一个"闪存“按钮
最近来博客园喜欢去闪存上看看,也就是一个类似微博的东西,但是貌似没看到哪里有这个按钮. 所以只要自己动手搞一个. 暴力猴js: // ==UserScript== // @name fwindpeak ...
- BZOJ 1014: [JSOI2008]火星人prefix Splay+二分
1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...
- Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees 暴力
A. Lala Land and Apple Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/cont ...
- Codeforces Round #215 (Div. 1) B. Sereja ans Anagrams 匹配
B. Sereja ans Anagrams Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- C#操作注册表全攻略
相信每个人对注册表并不陌生,在运行里面输入“regedit”就可以打开注册表编辑器了.这东西对Windows系统来说可是比较重要的,也是病 毒常常会光顾的地方,比如病毒和恶意软件常常会在注册表的启动项 ...
- xmf 翻译
避免在详细信息视图的确认对话框显示? https://documentation.devexpress.com/#Xaf/CustomDocument3160 我如何获得从登录窗口应用程序的数据库? ...
- php获取当前url完整地址
//获取当前访问的完整url地址 function getCurUrl() { $url = 'http://'; if (isset($_SERVER['HTTPS']) && $_ ...
- C++对象模型(虽然在GCC下很大的不同,但是先收藏)
C++对象模型 何为C++对象模型? 部分: 1. 语言中直接支持面向对象程序设计的部分 2. 对于各种支持的底层实现机制 语言中直接支持面向对象程序设计的部分,如构造函数 ...