How to determine sim card status and read contact information in android
When writing a program, sometimes you may need to obtain some contact information in the SIM card. Before obtaining the SIM card contacts, we usually determine the status of the SIM card first, and then obtain its information after finding the SIM card. With the following code, we can read some information of the contacts in the SIM card.
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); } }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
