一、参考
1、Android 获取手机的厂商、型号、Android系统版本号、IMEI、当前系统语言等工具类
2、Android获取SD卡总容量,可用大小,机身内存总容量及可用大小的系统方法
3、android 安卓APP获取手机设备信息和手机号码的代码示例
4、Android 获取本机号码
二、工具类
//===========================device system or app info=======================================
/**
* 获取当前手机系统语言。
*
* @return 返回当前系统语言。例如:当前设置的是“中文-中国”,则返回“zh-CN”
*/
public static String getSystemLanguage() {
return Locale.getDefault().getLanguage();
}
/**
* 获取当前系统上的语言列表(Locale列表)
*
* @return 语言列表
*/
public static Locale[] getSystemLanguageList() {
return Locale.getAvailableLocales();
}
/**
* 获取当前手机系统Sdk版本号
*
* @return Sdk系统版本号
*/
public static int getSystemSdkVersion() {
return Build.VERSION.SDK_INT;
}
/**
* 获取当前手机系统版本号
*
* @return 系统版本号
*/
public static String getSystemVersion() {
return android.os.Build.VERSION.RELEASE;
}
/**
* 获取手机型号
*
* @return 手机型号
*/
public static String getSystemModel() {
return android.os.Build.MODEL;
}
/**
* 获取手机厂商
*
* @return 手机厂商
*/
public static String getDeviceBrand() {
return android.os.Build.BRAND;
}
/**
* 获取手机IMEI(需要“android.permission.READ_PHONE_STATE”权限)
*
* @return 手机IMEI
*/
public static String getIMEI(Context ctx) throws SecurityException{
TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
if (tm != null) {
return tm.getDeviceId();
}
return null;
}
/**
* 获取应用程序名称
*
* @param context
* @return
*/
public static String getAppName(Context context) {
PackageManager packageManager = context.getPackageManager();
try {
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
int labelRes = packageInfo.applicationInfo.labelRes;
return context.getResources().getString(labelRes);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取应用程序版本名称信息
*
* @param context
* @return 当前应用的版本名称
*/
public static String getVersionName(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
return packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取应用程序的版本Code信息
*
* @param context
* @return 版本code
*/
public static int getVersionCode(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return 0;
}
/**
* android设备号,双清会改变
* @param context 上下文
* @return android设备号
*/
public static String getAndroidID(Context context){
return Settings.Secure.getString(context.getContentResolver(),Settings.Secure.ANDROID_ID);
}
/**
* android设备号,双清会改变
* @param context 上下文
* @return android设备号(组合型)
*/
public static String getDeviceId(Context context) throws SecurityException{
String deviceId = null;
deviceId = ((TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
if (deviceId == null && Build.VERSION.SDK_INT > 9) {
deviceId = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ANDROID_ID);
if (deviceId == null) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null
&& networkInfo.isAvailable()
&& networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
WifiManager wm = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
deviceId = wm.getConnectionInfo().getMacAddress();
} else {
deviceId = UUID.randomUUID().toString();
}
}
}
if (deviceId != null && deviceId.length() < 28) {
int len = 28 - deviceId.length();
for (int i = 0; i < len; i++) {
deviceId = "0" + deviceId;
}
}
return deviceId;
}
//=============================android's sdcard rom ram start===================================
private static final int ERROR = -1;
/**
* SDCARD是否存
*/
public static boolean externalMemoryAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
/**
* 获取手机内部剩余存储空间
*
* @return
*/
public static long getAvailableInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return availableBlocks * blockSize;
}
/**
* 获取手机内部总的存储空间
*
* @return
*/
public static long getTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return totalBlocks * blockSize;
}
/**
* 获取SDCARD剩余存储空间
*
* @return
*/
public static long getAvailableExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return availableBlocks * blockSize;
} else {
return ERROR;
}
}
/**
* 获取SDCARD总的存储空间
*
* @return
*/
public static long getTotalExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return totalBlocks * blockSize;
} else {
return ERROR;
}
}
/**
* 获取系统总内存
*
* @param context 可传入应用程序上下文。
* @return 总内存大单位为B。
*/
public static long getTotalMemorySize(Context context) {
String dir = "/proc/meminfo";
try {
FileReader fr = new FileReader(dir);
BufferedReader br = new BufferedReader(fr, 2048);
String memoryLine = br.readLine();
String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:"));
br.close();
return Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")) * 1024l;
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
/**
* 获取当前可用内存,返回数据以字节为单位。
*
* @param context 可传入应用程序上下文。
* @return 当前可用内存单位为B。
*/
public static long getAvailableMemory(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
am.getMemoryInfo(memoryInfo);
return memoryInfo.availMem;
}
private static DecimalFormat fileIntegerFormat = new DecimalFormat("#0");
private static DecimalFormat fileDecimalFormat = new DecimalFormat("#0.#");
/**
* 单位换算
*
* @param size 单位为B
* @param isInteger 是否返回取整的单位
* @return 转换后的单位
*/
public static String formatFileSize(long size, boolean isInteger) {
DecimalFormat df = isInteger ? fileIntegerFormat : fileDecimalFormat;
String fileSizeString = "0M";
if (size < 1024 && size > 0) {
fileSizeString = df.format((double) size) + "B";
} else if (size < 1024 * 1024) {
fileSizeString = df.format((double) size / 1024) + "K";
} else if (size < 1024 * 1024 * 1024) {
fileSizeString = df.format((double) size / (1024 * 1024)) + "M";
} else {
fileSizeString = df.format((double) size / (1024 * 1024 * 1024)) + "G";
}
return fileSizeString;
}
//==============================get phone number info ====================================
/**
* 获取当前版本号
* */
public static String getPhoneNumber(Context context) throws SecurityException{
TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
// String deviceid = tm.getDeviceId(); //获取智能设备唯一编号
String te1 = tm.getLine1Number(); //获取本机号码 :部分手机没有,因为供应商没有写进去
// String imei = tm.getSimSerialNumber(); //获得SIM卡的序号 :电信没有
// String imsi = tm.getSubscriberId(); //得到用户Id
return (te1!=null ? te1:"NA");
}
/**
* 获取手机服务商信息
* */
public static String getProvidersName(Context context) throws SecurityException{
String ProvidersName = "NA";
try {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String IMSI = tm.getSubscriberId();
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
ProvidersName = "中国移动";
} else if (IMSI.startsWith("46001")) {
ProvidersName = "中国联通";
} else if (IMSI.startsWith("46003")) {
ProvidersName = "中国电信";
}
} catch (Exception e) {
e.printStackTrace();
}
return ProvidersName;
}
三、权限
部分功能需要权限获得,补充下,android 6.0之后需要动态获取权限,具体查看权限管理的相关资料
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />