Allove Team 致力于移动应用的开发和服务(Android,J2ME,iphone,ophone)
« 不远处的智能移动设备 Linkify对象的使用——对输入String做出判断 »
2010年01月30日分享人生

140位读者

Android取得SIM卡内的信息——TelephonyManager的应用

这里主要用到Android API的TelephonyManager对象(Android.telephony.TelephonyManager)。以下源码就该对象的几个方法展示其用途。具体源码如下:

package com.mobile.allove.wfp;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.Service;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class TelephonyManagerTest extends ListActivity implements OnClickListener{
private Button showInfoButton;
private TelephonyManager mTelephonyManager;
ArrayList SIMInfo_name=null;//显示要显示的SIM信息的名称
ArrayList
SIMInfo_value=null;//显示要显示的SIM信息的名称的值
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

this.Init();
showInfoButton.setOnClickListener(this);
}
public void Init()
{
showInfoButton=(Button)this.findViewById(R.id.Button01);
mTelephonyManager=(TelephonyManager) this.getSystemService(Service.TELEPHONY_SERVICE);

SIMInfo_name=new ArrayList();
SIMInfo_value=new ArrayList
();

//首先显示SIM卡状态
SIMInfo_name.add(this.getResources().getString(R.string.SIM_state));
if(mTelephonyManager.getSimState()==TelephonyManager.SIM_STATE_READY)
{
SIMInfo_value.add(”SIM卡状态良好!”);
}
else if(mTelephonyManager.getSimState()==TelephonyManager.SIM_STATE_ABSENT)
{
SIMInfo_value.add(”SIM卡未插入!”);
}
else
{
SIMInfo_value.add(”SIM卡被锁定或未知的状态!”);
}

//SIM卡供货商代码
SIMInfo_name.add(this.getResources().getString(R.string.SIM_code));
if(mTelephonyManager.getSimSerialNumber().equals(”"))
{
SIMInfo_value.add(”SIM卡供货商代码无法取得”);
}
else
{
SIMInfo_value.add(mTelephonyManager.getSimSerialNumber());
}

//SIM卡供货商名称
SIMInfo_name.add(this.getResources().getString(R.string.SIM_factoryname));
if(mTelephonyManager.getSimOperatorName().equals(”"))
{
SIMInfo_value.add(”SIM卡供货商名称无法取得”);
}
else
{
SIMInfo_value.add(mTelephonyManager.getSimOperatorName());
}

//SIM卡国别
SIMInfo_name.add(this.getResources().getString(R.string.SIM_country));
if(mTelephonyManager.getSimCountryIso().equals(”"))
{
SIMInfo_value.add(”SIM卡国别无法取得”);
}
else
{
SIMInfo_value.add(mTelephonyManager.getSimCountryIso());
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.Button01:
this.setListAdapter(new MyAdapter());
break;
}
}
class Holder
{
TextView name_TextView;
TextView value_TextView;
}
class MyAdapter extends BaseAdapter
{
LayoutInflater inflater;
Holder mHolder;
@Override
public int getCount() {
// TODO Auto-generated method stub
return SIMInfo_name.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
inflater=LayoutInflater.from(TelephonyManagerTest.this);
if(convertView==null)
{
convertView=inflater.inflate(R.layout.item_layout,null);
mHolder=new Holder();
mHolder.name_TextView=(TextView)convertView.findViewById(R.id.item_TextView01);
mHolder.value_TextView=(TextView)convertView.findViewById(R.id.item_TextView02);
convertView.setTag(mHolder);
}
else
{
mHolder=(Holder) convertView.getTag();
}
mHolder.name_TextView.setText(SIMInfo_name.get(position));
mHolder.value_TextView.setText(SIMInfo_value.get(position));
return convertView;
}

}
}

注意加读取SIM的权限——android.permission.READ_PHONE_STATE

随机日志

日志信息 »

该日志于2010-01-30 14:04由 秦 涛 发表在分享人生分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下引用到你的网站或博客,还可以通过RSS 2.0订阅这个日志的所有评论。

没有评论

发表评论 »

返回顶部