android系统中使用TelephonyManager类来获取imei号和其他手机信息

在AndroidManifest.xml文件中增加
<!--允许读取电话状态SIM的权限-->
<uses-permission
android:name="android.permission.READ_PHONE_STATE" />
代码如下:
private
void getSystemPhoneMessage(){
 
  TelephonyManager
telephonyManager =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  //手机串号:GSM手机的
IMEI 和 CDMA手机的 MEID.
  String
deviceID = telephonyManager.getDeviceId();
  //手机号(有些手机号无法获取,是因为运营商在SIM中没有写入手机号)
  String
tel = telephonyManager.getLine1Number();
  //获取手机SIM卡的序列号
  String
imei =telephonyManager.getSimSerialNumber();
  //获取客户id,在gsm中是imsi号
  String
imsi =telephonyManager.getSubscriberId();
  //电话方位
  CellLocation
str = telephonyManager.getCellLocation();
  //运营商名称,注意:仅当用户已在网络注册时有效,在CDMA网络中结果也许不可靠
  String
networkoperatorName = telephonyManager.getNetworkOperatorName();
  //取得和语音邮件相关的标签,即为识别符
  String
voiceMail = telephonyManager.getVoiceMailAlphaTag();
  //获取语音邮件号码:
  String
voiceMailNumber = telephonyManager.getVoiceMailNumber();
  //获取ISO国家码,相当于提供SIM卡的国家码。
  String
simCountryIso = telephonyManager.getSimCountryIso();
 
  /**
 
* 电话状态:
 
* 1.tm.CALL_STATE_IDLE=0          无活动
 
* 2.tm.CALL_STATE_RINGING=1  响铃
 
* 3.tm.CALL_STATE_OFFHOOK=2  摘机
 
*/
  int
callState = telephonyManager.getCallState();
 
  /**
 
* 设备的软件版本号:
 
* 例如:the IMEI/SV(software version) for GSM phones.
 
* Return null if the software version is not available.
 
*/
  String
devicesoftware = telephonyManager.getDeviceSoftwareVersion();
 
  /**
 
* 获取ISO标准的国家码,即国际长途区号。
 
* 注意:仅当用户已在网络注册后有效。
 
*      在CDMA网络中结果也许不可靠。
  */ 
  String
networkCountry = telephonyManager.getNetworkCountryIso();
 
  /**
 
* MCC+MNC(mobile country code + mobile network code)
 
* 注意:仅当用户已在网络注册时有效。
 
*    在CDMA网络中结果也许不可靠。
 
*/ 
  String
networkoperator = telephonyManager.getNetworkOperator();
 
  /**
 
* 当前使用的网络类型:
 
* 例如: NETWORK_TYPE_UNKNOWN  网络类型未知  0
 
     NETWORK_TYPE_GPRS     GPRS网络  1
 
     NETWORK_TYPE_EDGE     EDGE网络  2
 
     NETWORK_TYPE_UMTS     UMTS网络  3
 
     NETWORK_TYPE_HSDPA    HSDPA网络  8
 
     NETWORK_TYPE_HSUPA    HSUPA网络  9
 
     NETWORK_TYPE_HSPA     HSPA网络  10
 
     NETWORK_TYPE_CDMA     CDMA网络,IS95A 或 IS95B.  4
 
     NETWORK_TYPE_EVDO_0   EVDO网络, revision 0.  5
 
     NETWORK_TYPE_EVDO_A   EVDO网络, revision A.  6
 
     NETWORK_TYPE_1xRTT    1xRTT网络  7
 
   */ 
  int
netWorkType = telephonyManager.getNetworkType();
 
  /**
 
* 手机类型:
 
* 例如: PHONE_TYPE_NONE  无信号
 
      PHONE_TYPE_GSM   GSM信号
 
         PHONE_TYPE_CDMA  CDMA信号
 
*/
  int
phoneType = telephonyManager.getPhoneType();
 
  /**
 
* 获取SIM卡提供的移动国家码和移动网络码.5或6位的十进制数字.
 
* SIM卡的状态必须是 SIM_STATE_READY(使用getSimState()判断).
  */
  String
simOperator = telephonyManager.getSimOperator();
 
  /**
 
  * 服务商名称:
 
  * 例如:中国移动、联通
 
  * SIM卡的状态必须是 SIM_STATE_READY(使用getSimState()判断).
  */
  String
simOperatorName = telephonyManager.getSimOperatorName();
 
  /**
 
* SIM的状态信息:
 
* SIM_STATE_UNKNOWN          未知状态 0
 
   SIM_STATE_ABSENT           没插卡 1
 
   SIM_STATE_PIN_REQUIRED     锁定状态,需要用户的PIN码解锁 2
 
   SIM_STATE_PUK_REQUIRED     锁定状态,需要用户的PUK码解锁 3
 
   SIM_STATE_NETWORK_LOCKED   锁定状态,需要网络的PIN码解锁 4
 
   SIM_STATE_READY            就绪状态 5
  */
  int
simStat = telephonyManager.getSimState();
 
  /**
 
* ICC卡是否存在
 
*/
  boolean
bl= telephonyManager.hasIccCard();
 
   /**
 
* 是否漫游:
 
* (在GSM用途下)
 
*/
  boolean
blean = telephonyManager.isNetworkRoaming();
  /**
 
* 附近的电话的信息:
 
* 类型:List<NeighboringCellInfo>
 
* 需要权限:android.Manifest.permission#ACCESS_COARSE_UPDATES
  */
  List<NeighboringCellInfo>
list =
telephonyManager.getNeighboringCellInfo();//List<NeighboringCellInfo>
  /**
 
* 获取数据连接状态
 
*/
  int
dataActivty = telephonyManager.getDataActivity();
 
}

 

 

eg:

 

目录结构

 

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:fillViewport="true"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView android:id="@+id/listView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </ScrollView>
</LinearLayout>
View Code

array.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="listItem">            
        <item>设备编号</item>
        <item>SIM卡国别</item>
        <item>SIM卡序列号</item>
        <item>SIM卡状态</item>
        <item>软件版本</item>
        <item>网络运营商代号</item>
        <item>网络运营商名称</item>
        <item>手机制式</item>
        <item>设备当前位置</item>
    </string-array>
    <string-array name="simState">            
        <item>状态未知</item>
        <item>无SIM卡</item>
        <item>被PIN加锁</item>
        <item>被PUK加锁</item>
        <item>被NetWork PIN加锁</item>
        <item>已准备好</item>
    </string-array>
    <string-array name="phoneType">            
        <item>未知</item>
        <item>GSM</item>
        <item>CDMA</item>
    </string-array>    
</resources>
View Code

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ljq.activity" android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon"
        android:label="@string/app_name">
        <activity android:name=".TelephonyManagerActivity"
            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="7" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>
View Code

TelephonyManagerActivity类

package com.ljq.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class TelephonyManagerActivity extends Activity {
    private ListView listView=null;
    private TelephonyManager tm=null;
    private String[] phoneType=null;
    private String[] simState=null;
    private String[] listItems=null;
    ArrayList<String> listValues=new ArrayList<String>();
    BaseAdapter adapter=new BaseAdapter(){

        public int getCount() {
            return listItems.length;
        }

        public Object getItem(int position) {
            return listItems[position];
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout ll=new LinearLayout(TelephonyManagerActivity.this);
            ll.setOrientation(LinearLayout.VERTICAL);
            TextView tvItem=new TextView(TelephonyManagerActivity.this);
            tvItem.setTextSize(24);
            tvItem.setText(listItems[position]);
            tvItem.setGravity(Gravity.LEFT);//设置在父容器中的对齐方式
            ll.addView(tvItem);
            TextView tvValue=new TextView(TelephonyManagerActivity.this);
            tvValue.setTextSize(18);                    //设置字体大小
            tvValue.setText(listValues.get(position));    //设置显示的内容
            tvValue.setPadding(0, 0, 10, 10);            //设置四周边界
            tvValue.setGravity(Gravity.RIGHT);    
            ll.addView(tvValue);
            return ll;
        }
        
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        listItems=getResources().getStringArray(R.array.listItem);
        simState=getResources().getStringArray(R.array.simState);
        phoneType=getResources().getStringArray(R.array.phoneType);
        tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        listView=(ListView)findViewById(R.id.listView);
        initListValues();
        listView.setAdapter(adapter);
    }
    
    /**
     * 获取各个数据项的值
     */
    public void initListValues(){            
        listValues.add(tm.getDeviceId());//获取设备编号
        listValues.add(tm.getSimCountryIso());//获取SIM卡国别
        listValues.add(tm.getSimSerialNumber());//获取SIM卡序列号    
        listValues.add(simState[tm.getSimState()]);//获取SIM卡状态
        listValues.add((tm.getDeviceSoftwareVersion()!=null?tm.getDeviceSoftwareVersion():"未知"));    //获取软件版本
        listValues.add(tm.getNetworkOperator());//获取网络运营商代号
        listValues.add(tm.getNetworkOperatorName());//获取网络运营商名称
        listValues.add(phoneType[tm.getPhoneType()]);//获取手机制式
        listValues.add(tm.getCellLocation().toString());//获取设备当前位置
    }
}
View Code

运行结果

 

posted @ 2013-08-12 18:32  无恨星晨  阅读(36854)  评论(0编辑  收藏  举报