MachineUtil.java 2.65 KB
package com.hjx.personalcenter.util;

import android.content.Context;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class MachineUtil {
	
	public static String getMachineCode(Context context) {
		String cpuAddress = null;
		try{
//			PmpsManager pmps = new PmpsManager();
//			cpuAddress = pmps.getDeviceId(context);
		}catch(Exception e){
			com.hjx.personalcenter.util.LogUtil.Instance().WriteLog("mac地址获取失败");
		}catch(NoClassDefFoundError e){
			com.hjx.personalcenter.util.LogUtil.Instance().WriteLog("设备不是好记星");
		}
		if(!TextUtils.isEmpty(cpuAddress) && !"A818A81800".equals(cpuAddress)){
			//Log.e("=========.MACHINE.=========", "function 1 get device id:"+cpuAddress);
			return cpuAddress;
		}

		try{
			cpuAddress = android.provider.Settings.System.getString(context.getContentResolver(), "serial_number").trim();
		}catch(Exception e){
			com.hjx.personalcenter.util.LogUtil.Instance().WriteLog("=========.MACHINE.=========function 2 get device id error");
		}
		if(!TextUtils.isEmpty(cpuAddress)){
			//Log.e("=========.MACHINE.=========", "function 2 get device id:"+cpuAddress);
			return cpuAddress;
		}

		try {
			if("Top".equals(Build.MODEL)){
				String str = "", strCPU = "";
				// 读取CPU信息
				Process pp = Runtime.getRuntime().exec("cat/proc/cpuinfo");
				InputStreamReader ir = new InputStreamReader(pp.getInputStream());
				LineNumberReader input = new LineNumberReader(ir);
				// 查找CPU序列号
				for (int i = 1; i < 100; i++) {
					str = input.readLine();
					if (str != null) {
						// 查找到序列号所在行
						if (str.indexOf("Serial") > -1) {
							// 提取序列号
							strCPU = str.substring(str.indexOf(":") + 1, str.length());
							cpuAddress = strCPU.trim();
							break;
						}
					} else {
						break;
					}
				}
			}
		} catch (Exception e) {
			com.hjx.personalcenter.util.LogUtil.Instance().WriteLog("=========.MACHINE.=========function 3 get device id error");
		}
		if(!TextUtils.isEmpty(cpuAddress)){
			//Log.e("=========.MACHINE.=========", "function 3 get device id:"+cpuAddress);
			return cpuAddress;
		}

		try{
			cpuAddress = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
		}catch(Exception e){
			com.hjx.personalcenter.util.LogUtil.Instance().WriteLog("=========.MACHINE.=========function 4 get device id error");
		}
		if(!TextUtils.isEmpty(cpuAddress)){
			//Log.e("=========.MACHINE.=========", "function 4 get device id:"+cpuAddress);
			return cpuAddress;
		}

		cpuAddress = "unknown";
		return cpuAddress;
	}
}