在開發app的時候,我們有時候需要獲取手機的一些硬件信息,比如說imei,mac地址之類的,下面我給個示例。
public class CommentUtil {public static Context context;
public static Application application;
public static void setContext(Context c){
context = c;
}
public static Context getContext(){
return context;
}
public void setApplication(Application a){
application = a;
}
public Application getApplication(){
return application;
}
//這裏是獲取手機的Imei值,imei是手機的一個身份號,可以說是每臺手機的唯一標識,具體可以自己google
TelephonyManager telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String imei = telManager.getDeviceId();
if(null == imei)
return "";
return imei;
}
//這裏是獲取手機的mac地址
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
String mac = wifiManager.getConnectionInfo().getMacAddress();
if(null == mac)
return "";
return mac;
}
//這裏是獲取應用的版本,其中versionName和versionCode是你在AndroidManifest.xml裏面設置的
String version;
try {
PackageInfo packageInfo = (PackageInfo)application.getPackageManager().getPackageInfo(application.getPackageName(), 0);
version = packageInfo.versionName;
version += ".";
version += packageInfo.versionCode;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
version = "";
}
return version;
}
//這裏是獲取手機的型號
String model = "";
model = android.os.Build.MODEL;
return model;
}
//這裏是獲取手機的生產產家
String manufacturer = "";
manufacturer = android.os.Build.MANUFACTURER;
return manufacturer;
}
}