`
心雨心
  • 浏览: 352255 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

读取SIM卡信息

阅读更多

通过TelephonyManager读取SIM卡信息:

AndroidManifest.xml中配置权限:
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="com.ek.test"

      android:versionCode="1"

      android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".SIMCard"

                  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-sdk android:minSdkVersion="3" />

    <uses-permission android:name = "android.permission.READ_PHONE_STATE"/>

</manifest>

主程序:import java.util.ArrayList;

import java.util.List;

import android.app.ListActivity;

import android.os.Bundle;

import android.telephony.TelephonyManager;

public class SIMCard extends ListActivity {

    /** Called when the activity is first created. */

private List<String> items;

private List<String> values;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        TelephonyManager tm = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);//取得相关系统服务

        items = new ArrayList();

        values = new ArrayList();

        items.add("SIM 卡状态");

        switch(tm.getSimState()){ //getSimState()取得sim的状态  有下面6中状态

        case TelephonyManager.SIM_STATE_ABSENT :values.add("无卡");break;

        case TelephonyManager.SIM_STATE_UNKNOWN :values.add("未知状态");break;

        case TelephonyManager.SIM_STATE_NETWORK_LOCKED :values.add("需要NetworkPIN解锁");break;

        case TelephonyManager.SIM_STATE_PIN_REQUIRED :values.add("需要PIN解锁");break;

        case TelephonyManager.SIM_STATE_PUK_REQUIRED :values.add("需要PUK解锁");break;

        case TelephonyManager.SIM_STATE_READY :values.add("良好");break;

        }

       

        items.add("卡号");

        if(tm.getSimSerialNumber()!=null){

         values.add(tm.getSimSerialNumber().toString());

        }else{

         values.add("无法取得SIM卡号");

        }

       

        items.add("供货商代码");

        if(tm.getSimOperator().equals("")){

         values.add("无法取得供货商代码");

        }else{

         values.add(tm.getSimOperator().toString());

        }

       

        items.add("供货商");

        if(tm.getSimOperatorName().equals("")){

         values.add("无法取得供货商");

        }else{

         values.add(tm.getSimOperatorName().toString());

        }

       

        items.add("国籍");

        if(tm.getSimCountryIso().equals("")){

         values.add("无法取得国籍");

        }else{

         values.add(tm.getSimCountryIso().toString());

        }

        setListAdapter(new DataAdapter(SIMCard.this,items,values));

    }

}
List的适配器import java.util.List;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.TextView;

public class DataAdapter extends BaseAdapter {

private List<String> items;

private List<String> values;

private LayoutInflater lif ;

public DataAdapter(Context context,List<String> items,List<String> values){

  this.items = items;

  this.values = values;

  lif = LayoutInflater.from(context);

}



public int getCount() {

  // TODO Auto-generated method stub

  return items.size();

}

public Object getItem(int arg0) {

  // TODO Auto-generated method stub

  return items.get(arg0);

}

public long getItemId(int position) {

  // TODO Auto-generated method stub

  return position;

}

public View getView(int position, View convertView, ViewGroup parent) {

  // TODO Auto-generated method stub

  MyView mv ;

  if(convertView == null){

   convertView = lif.inflate(R.layout.mylayout, null);//加载自定义的布局

/*设置自定义的布局*/

   mv = new MyView();

   mv.tv1 = (TextView)convertView.findViewById(R.id.item1);

   mv.tv2 = (TextView)convertView.findViewById(R.id.item2);

   convertView.setTag(mv);

  }else{

   mv = (MyView)convertView.getTag();

  }



  mv.tv1.setText(items.get(position).toString());

  mv.tv2.setText(values.get(position).toString());

  return convertView;

}

/*自定义的view里面只有两个属性 也就是两行显示 第一行是显示标题 第二行是显示的内容*/

private class MyView{

  TextView tv1;

  TextView tv2;

}

}

res/layout/mylayout.xml :list的布局

<?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"

    >

<TextView 

android:id="@+id/item1"

    android:layout_width="fill_parent"

    android:layout_height="25px"

  

    />

<TextView 

android:id="@+id/item2"

    android:layout_width="fill_parent"

    android:layout_height="25px"

  

    />

   

</LinearLayout>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics