TelephonyManger 是一个管理手机通话状态, 电话网络信息的服务类.该类 提供了大量的getXxx()方法来获取电话网络的相关信息.
获取 TelephonyManger 很简单,只要调用如下代码即可:
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
package com.test.telephony;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* TelephonyManger 电话管理器
*/
public class MainActivity extends Activity {
ListView mListView;
String[] statusNames; //声明代表状态名称的数组
//声明代表手机状态的集合
List<String> statusValues = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取 TelephonyManger 对象
TelephonyManager manger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//获取各种状态名称的数组
statusNames = getResources().getStringArray(R.array.statusNames);
//获取代表SIM卡状态的数组
String[] simStates = getResources().getStringArray(R.array.simState);
//h获取代表电话网络类型的数组
String[] phoneType = getResources().getStringArray(R.array.phoneType);
// statusValues.add(manger.getDeviceId()); //获取设备编号
try {
//获取手机系统版本号
statusValues.add(manger.getDeviceSoftwareVersion() != null ? manger.getDeviceSoftwareVersion() : "未知");
//获取网络运营商代号
statusValues.add(manger.getNetworkOperator());
//获取手机网络类型
statusValues.add(phoneType[manger.getPhoneType()]);
//获取网络运营商名称
statusValues.add(manger.getNetworkOperatorName());
//获取设备所在位置
statusValues.add(manger.getCellLocation() != null ? manger.getCellLocation().toString() : "未知位置");
//获取SIM卡的国别
statusValues.add(manger.getSimCountryIso());
//获取SIM卡的序列号
statusValues.add(manger.getSimSerialNumber());
//获取SIM卡的状态
statusValues.add(simStates[manger.getSimState()]);
} catch (Exception e) {
e.printStackTrace();
}
mListView = (ListView) findViewById(R.id.lv_phone_info);
ArrayList<Map<String,String>> status = new ArrayList<>();
//遍历 statusValues集合 将 statusValues statusNames 的数据封装到 ArrayList<Map<String,String>>集合中
for (int i = 0; i < statusValues.size(); i++) {
Map<String,String> map = new HashMap<>();
map.put("name",statusNames[i]);
map.put("value",statusValues.get(i));
status.add(map);
}
//使用 SimpleAdapter 封装list数据
// 使用SimpleAdapter封装List数据
SimpleAdapter adapter = new SimpleAdapter(this, status,
R.layout.item,
new String[] { "name", "value" },
new int[] { R.id.name, R.id.value});
//为 mListView 设置adapter
mListView.setAdapter(adapter);
}
}
布局界面就是一个listview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.test.telephony.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv_phone_info"/>
</RelativeLayout>
每个条目的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="120px"
android:background="#ff4"
android:textSize="16dip"
/>
<TextView
android:text="12334"
android:id="@+id/value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="8px"
android:textSize="16dip"
/>
</LinearLayout>

本文介绍如何使用TelephonyManager类获取Android设备的详细信息,包括系统版本、网络运营商、网络类型等,并展示了完整的代码示例。
1687

被折叠的 条评论
为什么被折叠?



