android手机获取手机设备信息

在有的项目中需要根据特定的手机信息来进行处理,这时就需要我们来获取手机的设备信息了,那首先看看我的测试机的一些基本信息:

android手机获取手机设备信息_第1张图片

那么这些信息怎么获得呢??其实都封装在了TelephonyManager中,我们从里面可以拿到,但是注意的是有的手机手机号是拿不到的(例如我的nexus 5X)

好了,代码如下:

封装好的PhoneInfo 类

package com.ddv.www.shadowphone.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.view.Display;
import android.view.WindowManager;

/**
 * 读取手机设备信息测试代码
 */
public class PhoneInfo {

    private static TelephonyManager tm;

    /**
     * 获取SIM硬件信息
     *
     * @return
     */
    public static TelephonyManager getTelephonyManager() {
        if (tm == null)
            tm = (TelephonyManager) UIUtils.getContext().getSystemService(Context.TELEPHONY_SERVICE);
//        StringBuffer sb = new StringBuffer();
//        sb.append("\nDeviceId(IMEI) = " + tm.getDeviceId());
//        sb.append("\nDeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion());
//        sb.append("\nLine1Number = " + tm.getLine1Number());
//        sb.append("\nNetworkCountryIso = " + tm.getNetworkCountryIso());
//        sb.append("\nNetworkOperator = " + tm.getNetworkOperator());
//        sb.append("\nNetworkOperatorName = " + tm.getNetworkOperatorName());
//        sb.append("\nNetworkType = " + tm.getNetworkType());
//        sb.append("\nPhoneType = " + tm.getPhoneType());
//        sb.append("\nSimCountryIso = " + tm.getSimCountryIso());
//        sb.append("\nSimOperator = " + tm.getSimOperator());
//        sb.append("\nSimOperatorName = " + tm.getSimOperatorName());
//        sb.append("\nSimSerialNumber = " + tm.getSimSerialNumber());
//        sb.append("\nSimState = " + tm.getSimState());
//        sb.append("\nSubscriberId(IMSI) = " + tm.getSubscriberId());
//        sb.append("\nVoiceMailNumber = " + tm.getVoiceMailNumber());
//        LogUtils.i(sb.toString());
        return tm;
    }

    /**
     * 获取屏幕分辨率
     *
     * @return
     */
    public static int[] getMetrics() {
        WindowManager wm = (WindowManager) UIUtils.getContext().getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        int width = point.x;
        int height = point.y;
        int[] metrics = {width, height};
        return metrics;
    }

    /**
     * 设备厂商
     *
     * @return
     */
    public static String getPhoneBrand() {
        return Build.BOARD + "  " + Build.MANUFACTURER;
    }

    /**
     * 设备名称
     *
     * @return
     */
    public static String getPhoneModel() {
        return Build.MODEL;
    }

    /**
     * 得到软件版本号
     *
     * @param context 上下文
     * @return 当前版本Code
     */
    public static int getVerCode(Context context) {
        int verCode = -1;
        try {
            String packageName = context.getPackageName();
            verCode = context.getPackageManager()
                    .getPackageInfo(packageName, 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return verCode;
    }

    /**
     * 获得APP名称
     *
     * @param context
     * @return
     */
    public static String getAppName(Context context) {
        String appName = "";
        try {
            PackageManager packageManager = context.getPackageManager();
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
            appName = (String) packageManager.getApplicationLabel(applicationInfo);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return appName;
    }
}  

拿到相应的信息:

 //制造商: 
 tvManufacturer.setText(getString(R.string.manufacturer) + PhoneInfo.getPhoneBrand());

 //设备型号:   
     tvEquipmentModel.setText(getString(R.string.equipment_model) + PhoneInfo.getPhoneModel());

   //设备分辨率  
       tvDeviceResolution.setText(getString(R.string.device_resolution) + PhoneInfo.getMetrics()[0] + "x" +
                PhoneInfo.getMetrics()[1]);

        //ISDN:

        tvMobileIsdn.setText(getString(R.string.mobile_isdn) + PhoneInfo.getTelephonyManager().getLine1Number());

    //IMEI

tvMobileImei.setText(getString(R.string.mobile_imei) + PhoneInfo.getTelephonyManager().getDeviceId());

    //IMSI

tvMobileImsi.setText(getString(R.string.mobile_imsi) + PhoneInfo.getTelephonyManager().getSubscriberId());

//SIM卡串号

    tvSimNumber.setText(getString(R.string.sim_number) + PhoneInfo.getTelephonyManager().getSimSerialNumber());

上面手机号是测试用号,不用打,不通的….哈哈哈

你可能感兴趣的:(android手机系统分析,和基站定位实现,基站开发)