Androidでsimカードの状態を判断し、連絡先資料を読み取る方法
13095 ワード
ライターでは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 += "
";
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 + "
";
return ;
}
mString += " :" + mCursor.getColumnName(0) + "
";
mString += " :" + mCursor.getColumnName(1) + "
";
mString += " :" + mCursor.getColumnName(2) + "
";
mString += " :" + mCursor.getColumnName(3) + "
";
mString += " :" + mCursor.getColumnCount() + "
";
mString += " :" + mCursor.getCount() + "
";
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)+"
";
}
}
mString += mCursor + "
";
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>