Service之电话管理器之获取网络和SIM卡信息

        TelephonyManager是一个管理手机通话状态,电话网络信息的服务类,提供了大量的getXxx()方法来获取电话网络的相关信息。

实例:获取网络和SIM卡信息

主界面文件包含一个ListView,用于显示获得的信息。

界面布局文件、数组资源文件略去。


TelephonyStatus.java代码:

package com.example.telephonystatus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class TelephonyStatus extends Activity {
	ListView showView;
	//声明代表状态名的数组
	String[] statusNames;
	//声明代表手机状态的集合
	ArrayList<String> statusValues = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_telephony_status);
        //获取系统的TelephonyManager对象
        TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        //获取各种状态名称的数组
        statusNames = getResources().getStringArray(R.array.statusNames);
        //获取代表SIM卡状态的数组
        //String[] simState = get
        //获取设备编号
        statusValues.add(telephonyManager.getDeviceId());
        //获取系统平台的版本
        statusValues.add(telephonyManager.getDeviceSoftwareVersion() != null ? telephonyManager.getDeviceSoftwareVersion() : "未知");
        //获取网络运营商代号
        statusValues.add(telephonyManager.getNetworkOperator());
        //获取网络运营商名称
        statusValues.add(telephonyManager.getNetworkOperatorName());
        //获取设备所在的位置
        statusValues.add(telephonyManager.getCellLocation() != null ? telephonyManager.getCellLocation().toString() : "未知位置");
        //获取SIM卡的国别
        statusValues.add(telephonyManager.getSimCountryIso());
        //获取SIM卡的序列号
        statusValues.add(telephonyManager.getSimSerialNumber());
        
        //获得ListView对象
        showView = (ListView) findViewById(R.id.listView);
        ArrayList<Map<String, String>> status = new ArrayList<Map<String,String>>();
        //遍历statusValues集合,将statusNames、statusValues的数据封装到List<Map<String,String>>集合中
        for (int i = 0; i < statusValues.size(); i++) {
        	HashMap<String, String> map = new HashMap<String, String>();
        	map.put("name", statusNames[i]);
        	map.put("value", statusValues.get(i));
        	status.add(map);
        }
        //使用SimpleAdapter封装List数据
        SimpleAdapter adapter = new SimpleAdapter(this, status, R.layout.line, new String[]{"name","value"}, new int[]{R.id.name,R.id.value});
        showView.setAdapter(adapter);
        
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值