1. 程式人生 > >android手機獲取手機裝置資訊

android手機獲取手機裝置資訊

在有的專案中需要根據特定的手機資訊來進行處理,這時就需要我們來獲取手機的裝置資訊了,那首先看看我的測試機的一些基本資訊:

這裡寫圖片描述

那麼這些資訊怎麼獲得呢??其實都封裝在了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());

上面手機號是測試用號,不用打,不通的….哈哈哈