Android 电话管理器TelephonyManager,获取网络,SIM卡信息

// 获取系统TelephonyManager对象
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

AndroidManifest.xml

package shortcut.song.com.myapplication;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
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.Map;

public class TelephonyStatusActivity extends AppCompatActivity {

    ListView showView;
    // 声明代表状态名的数组
    String[] statusNames;
    // 声明代表手机状态的集合
    ArrayList<String> statusValues = new ArrayList<>();
    @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 = getResources().getStringArray(
                R.array.simState);
        // 获取代表电话网络类型的数组
        String[] phoneType = getResources().getStringArray(
                R.array.phoneType);
        // 获取设备编号
        statusValues.add(telephonyManager.getDeviceId());
        // 获取系统平台的版本
        statusValues.add(telephonyManager.getDeviceSoftwareVersion() != null? telephonyManager.getDeviceSoftwareVersion(): "Unknow!");
        // 获取网络运营商代号
        statusValues.add(telephonyManager.getNetworkOperator());
        // 获取网络运营商名称
        statusValues.add(telephonyManager.getNetworkOperatorName());
        // 获取手机网络类型
        statusValues.add(phoneType[telephonyManager.getPhoneType()]);
        // 获取设备所在位置
        statusValues.add(telephonyManager.getCellLocation() != null ? telephonyManager.getCellLocation().toString() : "Unknow Location!");
        // 获取SIM卡的国别
        statusValues.add(telephonyManager.getSimCountryIso());
        // 获取SIM卡序列号
        statusValues.add(telephonyManager.getSimSerialNumber());
        // 获取SIM卡状态
        statusValues.add(simState[telephonyManager.getSimState()]);

        // 获得ListView对象
        showView = (ListView) findViewById(R.id.phone_show);
        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.telephony_line
                , new String[]{"name" , "value"}
                , new int[]{R.id.name , R.id.value});
        // 为ListView设置Adapter
        showView.setAdapter(adapter);


    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SongYuLong的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值