获得手机信息工具类

(原文出处:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=157038&fromuid=487937]http://www.eoeandroid.com/forum. ... 7038&fromuid=487937   感谢分享)

闲暇时整理了获取手机信息的工具类,通过这个类,我们可以获得以下手机信息:

1. 手机的IMEI
2.手机的制式类型,GSM OR CDMA 手机
3.手机网络国家编码
4.手机网络运营商ID。
5.手机网络运营商名称
6.手机的数据链接类型
7.是否有可用数据链接
8.当前的数据链接类型
9.手机剩余内存
10.手机总内存
11.手机CPU型号
12.手机名称
13.手机型号
14.手机设备制造商名称

源码如下:

 

package com.joy.wifi;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import android.Manifest;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.app.ActivityManager.MemoryInfo;
/**
 * @ClassName: PhoneInfo 
 * @Description: 获取手机信息的工具类
	1.手机的IMEI
	2.手机的制式类型,GSM OR CDMA 手机  /2.1手机的系统版本信息
	3.手机网络国家编码
	4.手机网络运营商ID。
	5.手机网络运营商名称
	6.手机的数据链接类型
	7.是否有可用数据链接
	8.当前的数据链接类型
	9.手机剩余内存
	10.手机总内存
	11.手机CPU型号
	12.手机名称
	13.手机型号
	14.手机设备制造商名称
	
	注意:需要如下权限
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
*/

public class PhoneInfo {
	private static final String TAG = PhoneInfo.class.getSimpleName();
	private static final String FILE_MEMORY = "/proc/meminfo";
	private static final String FILE_CPU = "/proc/cpuinfo";
	public String mIMEI;
	public int mPhoneType;
	public int mSysVersion;
	public String mNetWorkCountryIso;
	public String mNetWorkOperator;
	public String mNetWorkOperatorName;
	public int mNetWorkType;
	public boolean mIsOnLine;
	public String mConnectTypeName;
	public long mFreeMem;
	public long mTotalMem;
	public String mCupInfo;
	public String mProductName;
	public String mModelName;
	public String mManufacturerName;
	
	/**
	 * private constructor
	 */
	private PhoneInfo() {

	}

	/**
	 * get imei
	 * @return 手机的IMEI
	 */
	public static String getIMEI(Context context) {
		TelephonyManager manager = (TelephonyManager) context.getSystemService(Activity.TELEPHONY_SERVICE);
		// check if has the permission
		if (PackageManager.PERMISSION_GRANTED == context.getPackageManager()
		.checkPermission(Manifest.permission.READ_PHONE_STATE, context.getPackageName())) {
			return manager.getDeviceId();
		} else {
			return null;
		}
	}

	/**
	 * get phone type,like :GSM,CDMA,SIP,NONE
	 * @param context
	 * @return 手机的制式类型,GSM OR CDMA 手机  1
	 */
	public static int getPhoneType(Context context) {
		TelephonyManager manager = (TelephonyManager) context
		.getSystemService(Activity.TELEPHONY_SERVICE);
		return manager.getPhoneType();
	}

	/**
	 * get phone sys version
	 * @return 手机的系统版本信息. 9
	 */
	public static int getSysVersion() {
		return Build.VERSION.SDK_INT;
	}

	/**
	 * Returns the ISO country code equivalent of the current registered
	 * operator's MCC (Mobile Country Code).
	 * @param context
	 * @return 手机网络国家编码 cn
	 */
	public static String getNetWorkCountryIso(Context context) {
		TelephonyManager manager = (TelephonyManager) context.getSystemService(Activity.TELEPHONY_SERVICE);
		return manager.getNetworkCountryIso();
	}

	/**
	 * Returns the numeric name (MCC+MNC) of current registered operator.may not
	 * work on CDMA phone
	 * @param context
	 * @return 手机网络运营商ID。46001
	 */
	public static String getNetWorkOperator(Context context) {
		TelephonyManager manager = (TelephonyManager) context
		.getSystemService(Activity.TELEPHONY_SERVICE);
		return manager.getNetworkOperator();
	}

	/**
	 * Returns the alphabetic name of current registered operator.may not work
	 * on CDMA phone
	 * @param context
	 * @return 手机网络运营商名称 china unicom
	 */
	public static String getNetWorkOperatorName(Context context) {
		TelephonyManager manager = (TelephonyManager) context
		.getSystemService(Activity.TELEPHONY_SERVICE);
		return manager.getNetworkOperatorName();
	}

	/**
	 * get type of current network
	 * @param context
	 * @return 手机的数据链接类型 3
	 */
	public static int getNetworkType(Context context) {
		TelephonyManager manager = (TelephonyManager) context
		.getSystemService(Activity.TELEPHONY_SERVICE);

		return manager.getNetworkType();
	}

	/**
	 * is webservice aviliable
	 * @param context
	 * @return 是否有可用数据链接 true
	 */
	public static boolean isOnline(Context context) {
		ConnectivityManager manager = (ConnectivityManager) context
		.getSystemService(Activity.CONNECTIVITY_SERVICE);
		NetworkInfo info = manager.getActiveNetworkInfo();
		if (info != null && info.isConnected()) {
			return true;
		}
		return false;
	}

	/**
	 * get current data connection type name ,like ,Mobile/WIFI/OFFLINE
	 * @param context
	 * @return 当前的数据链接类型 wifi
	 */
	public static String getConnectTypeName(Context context) {
		if (!isOnline(context)) {
			return "OFFLINE";
		}
		ConnectivityManager manager = (ConnectivityManager) context
		.getSystemService(Activity.CONNECTIVITY_SERVICE);

		NetworkInfo info = manager.getActiveNetworkInfo();
		if (info != null) {
			return info.getTypeName();
		} else {
			return "OFFLINE";
		}
	}

	/**
	 * get free memory of phone, in M
	 * @param context
	 * @return 手机剩余内存 1024M
	 */
	public static long getFreeMem(Context context) {
		ActivityManager manager = (ActivityManager) context
		.getSystemService(Activity.ACTIVITY_SERVICE);

		MemoryInfo info = new MemoryInfo();
		manager.getMemoryInfo(info);
		long free = info.availMem / 1024 / 1024;

		return free;
	}

	/**
	 * get total memory of phone , in M
	 * @param context
	 * @return 手机总内存 2048M
	 */
	public static long getTotalMem(Context context) {
		try {
			FileReader fr = new FileReader(FILE_MEMORY);
			BufferedReader br = new BufferedReader(fr);
			String text = br.readLine();
			String[] array = text.split("\\s+");
			Log.w(TAG, text);

			return Long.valueOf(array[1]) / 1024;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return -1;
	}

	/**
	 * get the cpu info
	 * @return 手机CPU型号 ARMv7 Processor
	 */
	public static String getCpuInfo() {
		try {
			FileReader fr = new FileReader(FILE_CPU);
			BufferedReader br = new BufferedReader(fr);
			String text = br.readLine();
			String[] array = text.split(":\\s+", 2);
			for (int i = 0; i < array.length; i++) {
				Log.w(TAG, " .....  " + array[i]);
			}
			Log.w(TAG, text);
			return array[1];
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * get product name of phone
	 * @return 手机名称 libra_mione_plus
	 */
	public static String getProductName() {
		return Build.PRODUCT;
	}

	/**
	 * get model of phone
	 * @return  手机型号 MI-ONE Plus
	 */
	public static String getModelName() {

		return Build.MODEL;
	}

	/**
	 * get Manufacturer Name of phone
	 * @return 手机设备制造商名称 xiaomi
	 */
	public static String getManufacturerName() {
		return Build.MANUFACTURER;
	}

	//以下测试用
	public static PhoneInfo getPhoneInfo(Context context) {
		PhoneInfo result = new PhoneInfo();

		result.mIMEI = getIMEI(context);

		result.mPhoneType = getPhoneType(context);

		result.mSysVersion = getSysVersion();

		result.mNetWorkCountryIso = getNetWorkCountryIso(context);

		result.mNetWorkOperator = getNetWorkOperator(context);

		result.mNetWorkOperatorName = getNetWorkOperatorName(context);

		result.mNetWorkType = getNetworkType(context);

		result.mIsOnLine = isOnline(context);

		result.mConnectTypeName = getConnectTypeName(context);

		result.mFreeMem = getFreeMem(context);

		result.mTotalMem = getTotalMem(context);

		result.mCupInfo = getCpuInfo();

		result.mProductName = getProductName();

		result.mModelName = getModelName();

		result.mManufacturerName = getManufacturerName();

		return result;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();

		builder.append("IMEI : " + mIMEI + "\n");

		builder.append("mPhoneType : " + mPhoneType + "\n");

		builder.append("mSysVersion : " + mSysVersion + "\n");

		builder.append("mNetWorkCountryIso : " + mNetWorkCountryIso + "\n");

		builder.append("mNetWorkOperator : " + mNetWorkOperator + "\n");

		builder.append("mNetWorkOperatorName : " + mNetWorkOperatorName + "\n");

		builder.append("mNetWorkType : " + mNetWorkType + "\n");

		builder.append("mIsOnLine : " + mIsOnLine + "\n");

		builder.append("mConnectTypeName : " + mConnectTypeName + "\n");

		builder.append("mFreeMem : " + mFreeMem + "M\n");

		builder.append("mTotalMem : " + mTotalMem + "M\n");

		builder.append("mCupInfo : " + mCupInfo + "\n");

		builder.append("mProductName : " + mProductName + "\n");

		builder.append("mModelName : " + mModelName + "\n");

		builder.append("mManufacturerName : " + mManufacturerName + "\n");

		return builder.toString();

	}
}

注意:需要如下权限

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



源文件:http://download.csdn.net/detail/zmpcwl/5136513

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值