取得电信网络与手机的相关信息

要取得SIM卡信息状态以及相关信息可以使用TelephonyManager对象来获取。

当然除了SIM卡信息外,TelephonyManager还可以用来取得电信网络的相关信息。例如电信网络国别、电信公司代码、电信公司名称和网络类型等等。甚至还可以取得每部手机的唯一的IMEI码与IMSI码。

除此之外,如果要取得手机内的设置值,如蓝牙、无线网络的设置状态,还可以通过Android API提供的android.provider.Settings.System.对象来取得。


当然activity_main.xml中首先需要有ListView

activity_main.xml

<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"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

item中只是用来显示title和详细信息

telephone_info_items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tel_title"
        android:layout_width="wrap_content"
        android:layout_height="18sp"
        android:layout_marginTop="5sp"
        android:text="title"
        android:textColor="@android:color/black"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/tel_info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:text="info"
        android:textSize="22sp" />

</LinearLayout>

接下来是代码的实现

程序中以getSystemService(TELEPHONY_SERVICE)取得TelephonyManager对象,再通过TelephonyManager提供的方法取得电信网络相关信息。

另外,程序以android.provider.Settings.System.getString取得手机相关设置信息,并将取得的信息展示在ListView中。

MainActivity.java

package com.demo.telephoneInfo;

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

import android.app.Activity;
import android.content.ContentResolver;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {

	private TelephonyManager telMgr = null;
	private SimpleAdapter simpleAdapter = null;
	private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
	private ListView lv = null;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		lv = (ListView) findViewById(R.id.listView1);

		telMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

		Map<String, String> map = new HashMap<String, String>();
		// 手机号码
		map.put("tel_title", "本机号码");
		if (telMgr.getLine1Number() != null) {
			map.put("tel_info", "开始"+telMgr.getLine1Number()+"结束");
		} else {
			map.put("tel_info", "无法获取");
		}
		list.add(map);

		// 取得电信网络国别
		 map = new HashMap<String, String>();
		map.put("tel_title", "电信网络国别");
		if (telMgr.getNetworkCountryIso() != null) {
			map.put("tel_info", telMgr.getNetworkCountryIso());
		} else {
			map.put("tel_info", "无法获取");
		}
		list.add(map);

		// 获取电信公司代码
		 map = new HashMap<String, String>();
		map.put("tel_title", "电信公司代码");
		if (telMgr.getNetworkOperator() != null) {
			map.put("tel_info", telMgr.getNetworkOperator());
		} else {
			map.put("tel_info", "无法获取");
		}
		list.add(map);

		// 获取电信公司名称
		 map = new HashMap<String, String>();
		map.put("tel_title", "电信公司名称");
		if (telMgr.getNetworkOperatorName() != null) {
			map.put("tel_info", telMgr.getNetworkOperatorName());
		} else {
			map.put("tel_info", "无法获取");
		}
		list.add(map);

		// 获取移动通信类型
		 map = new HashMap<String, String>();
		map.put("tel_title", "移动通信类型");
		if (telMgr.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
			map.put("tel_info", "GSM");
		} else if (telMgr.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
			map.put("tel_info", "CDMA");
		} else if (telMgr.getPhoneType() == TelephonyManager.PHONE_TYPE_SIP) {
			map.put("tel_info", "SIP");
		} else {
			map.put("tel_info", "未知");
		}
		list.add(map);

		// 获取网络类型
		 map = new HashMap<String, String>();
		map.put("tel_title", "网络类型");
		if (telMgr.getNetworkType() == TelephonyManager.NETWORK_TYPE_EDGE) {
			map.put("tel_info", "EDGE");
		} else if (telMgr.getNetworkType() == TelephonyManager.NETWORK_TYPE_GPRS) {
			map.put("tel_info", "GPRS");
		} else if (telMgr.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS) {
			map.put("tel_info", "UMTS");
		} else if (telMgr.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSDPA) {
			map.put("tel_info", "HSDPA");
		} else {
			map.put("tel_info", "未知");
		}
		list.add(map);

		// 获取漫游状态
		 map = new HashMap<String, String>();
		map.put("tel_title", "漫游状态");
		if (telMgr.isNetworkRoaming()) {
			map.put("tel_info", "漫游中");
		} else {
			map.put("tel_info", "无漫游");
		}
		list.add(map);

		// 获取手机IMEI
		 map = new HashMap<String, String>();
		map.put("tel_title", "手机IMEI");
		if (telMgr.getDeviceId() != null) {
			map.put("tel_info", telMgr.getDeviceId());
		} else {
			map.put("tel_info", "无法取得");
		}
		list.add(map);

		// 获取手机IMEI SoftwareVersion
		 map = new HashMap<String, String>();
		map.put("tel_title", "手机IMEI SoftwareVersion");
		if (telMgr.getDeviceSoftwareVersion() != null) {
			map.put("tel_info", telMgr.getDeviceSoftwareVersion());
		} else {
			map.put("tel_info", "无法取得");
		}
		list.add(map);

		// 获取手机IMSI
		 map = new HashMap<String, String>();
		map.put("tel_title", "手机IMSI");
		if (telMgr.getSubscriberId() != null) {
			map.put("tel_info", telMgr.getSubscriberId());
		} else {
			map.put("tel_info", "无法取得");
		}
		list.add(map);

		// 取得ContentResolver
		ContentResolver cv = this.getContentResolver();
		String tmpStr = null;

		// 获取蓝牙状态
		 map = new HashMap<String, String>();
		map.put("tel_title", "蓝牙状态");
		tmpStr = android.provider.Settings.System.getString(cv,
				android.provider.Settings.System.BLUETOOTH_ON);
		if ("1".equals(tmpStr)) {
			map.put("tel_info", "已打开");
		} else {
			map.put("tel_info", "未打开");
		}
		list.add(map);

		// 取得数据漫游是否打开
		 map = new HashMap<String, String>();
		map.put("tel_title", "数据漫游");
		tmpStr = android.provider.Settings.System.getString(cv,
				android.provider.Settings.System.DATA_ROAMING);
		if ("1".equals(tmpStr)) {
			map.put("tel_info", "已打开");
		} else {
			map.put("tel_info", "未打开");
		}
		list.add(map);

		// 取得WIFI数据
		 map = new HashMap<String, String>();
		map.put("tel_title", "WIFI数据");
		tmpStr = android.provider.Settings.System.getString(cv,
				android.provider.Settings.System.WIFI_ON);
		if ("1".equals(tmpStr)) {
			map.put("tel_info", "已打开");
		} else {
			map.put("tel_info", "未打开");
		}
		list.add(map);

		// 取得飞行模式
		 map = new HashMap<String, String>();
		map.put("tel_title", "飞行模式");
		tmpStr = android.provider.Settings.System.getString(cv,
				android.provider.Settings.System.AIRPLANE_MODE_ON);
		if ("1".equals(tmpStr)) {
			map.put("tel_info", "已打开");
		} else {
			map.put("tel_info", "未打开");
		}
		list.add(map);

		simpleAdapter = new SimpleAdapter(this, list,
				R.layout.telephone_info_items, new String[] { "tel_title",
						"tel_info" },
				new int[] { R.id.tel_title, R.id.tel_info });

		lv.setAdapter(simpleAdapter);

		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Toast.makeText(
						MainActivity.this,
						list.get(arg2).get("tel_title") + "的状态/内容为"
								+ list.get(arg2).get("tel_info"),
						Toast.LENGTH_LONG).show();

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

做完以上基本程序已经开发完毕。附上权限即可。

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

效果如图



有一问题,真机测试电话号码无法显示,希望大佬告知一下。谢谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值