android系统api给我们提供了非常多的方法,去获取手机的一些信息。比如:任务栈,设备名称,Mac地址,IMEI,CUP信息,内存,外置存储卡;应用名称,版本信息,屏幕宽高,系统版本号,sdk版本等。逐一总结。
应用场景:如果前台界面为A activity,则做一些xx处理。
/** * 检测某Activity是否在Task的栈顶 * cmdName表示manifests里面注册的activity名称:com.xxx.xx.activity * 可以WebViewActivity.class.getName();获得 */
public static boolean isTopActivity(String cmdName,Context context){
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
String cmpNameTemp = null;
if(null != runningTaskInfos){
cmpNameTemp=(runningTaskInfos.get(0).topActivity).getClassName();
}
if(null == cmpNameTemp)return false;
return cmpNameTemp.equals(cmdName);
}
packageManager,包的相关信息都在这里面,可以获取packageInfo,applicationInfo等,比如:
// 获取当前的应用名称
public static String getApplicationName(Context context) {
PackageManager packageManager = null;
ApplicationInfo applicationInfo = null;
try {
packageManager = context.getApplicationContext().getPackageManager();
applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
applicationInfo = null;
}
String applicationName =
(String) packageManager.getApplicationLabel(applicationInfo);
return applicationName;
}
另外解析本地apk文件的版本信息方法如下:
//解析apkFile.apk文件版本
final PackageManager packageManager =mContext.getPackageManager();
final PackageInfo packageArchiveInfo = packageManager.getPackageArchiveInfo(apkFile.getAbsolutePath(), PackageManager.GET_ACTIVITIES);//File apkFile ...
final int versionCode = packageArchiveInfo.versionCode;
final String packageName = packageArchiveInfo.packageName;
VersionName,VersionCode,就是对应androidmanifest.xml中,或者build.gradle中定义的版本信息。
/** * 获取app版本信息 * @return */
public static String getVersionName(Context context) {
PackageManager pm = context.getPackageManager();
try {
PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
//返回版本名词
return packageInfo.versionName;//packageInfo.versionCode为版本号
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return "";
}
/*gradle配置: defaultConfig { minSdkVersion 9 targetSdkVersion 22 versionCode 1 versionName "1.0" }*/
获取用户的手机名称创建默认的用户名,文件夹什么的。
public static String getPhoneName(Context context){
Random random = new Random();
int rannum = (int) (random.nextDouble() * (999 - 100 + 1)) + 100;// 获取3位随机数
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
//return manufacturer+"_"+model + "_" + rannum;
return manufacturer+"_"+model;
}
很大应用都有绑定硬件的需求,一般的加密方式会选择绑定mac地址,作为用户设备区分。
/** 获取当前设备的MAC地址 */
public static String getMacAddress() {
Context context = UIUtils.getContext();
if (null == context) {
return null;
}
String mac = null;
try {
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wm.getConnectionInfo();
mac = info.getMacAddress();
} catch (Exception e) {
LogUtils.e(e);
}
return mac;
}
public static int getWinWidth(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//int width = wm.getDefaultDisplay().getWidth();
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int width= dm.widthPixels;//dm.heightPixels,高度
return width;
}
/** 获取设备的IMEI */
public static String getIMEI() {
Context context = UIUtils.getContext();
if (null == context) {
return null;
}
String imei = "";
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
imei = tm.getDeviceId();
} catch (Exception e) {
LogUtils.debug(e.toString());
}
return imei;
}
读取系统熟悉,执行Linux指令,都可以获取到很多有用的硬件信息,Linux指令太多了,有空另外总结介绍。
/** 获取手机总内存,单位为byte */
public static long getTotalMemory() {
long size = 0;
String path = "/proc/meminfo";// 系统内存信息文件
try {
String totalMemory = FileUtils.readProperties(path, "MemTotal", null);// 读出来是带单位kb的,并且单位前有空格,所以去掉最后三位
if (!TextUtils.isEmpty(totalMemory) && totalMemory.length() > 3) {
size = Long.valueOf(totalMemory.substring(0, totalMemory.length() - 3)) * 1024;
}
} catch (Exception e) {
LogUtils.e(e);
}
return size;
}
/** 获取手机内部总的空间大小,单位为byte */
@SuppressWarnings("deprecation")
public static long getTotalInternalSpace() {
long totalSpace = -1L;
try {
String path = Environment.getDataDirectory().getPath();
StatFs stat = new StatFs(path);
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();// 获取该区域可用的文件系统数
totalSpace = totalBlocks * blockSize;
} catch (Exception e) {
LogUtils.e(e);
}
return totalSpace;
}
/** 获取手机内部可用空间大小,单位为byte */
@SuppressWarnings("deprecation")
public static long getAvailableInternalMemorySize() {
long availableSpace = -1l;
try {
String path = Environment.getDataDirectory().getPath();// 获取 Android 数据目录
StatFs stat = new StatFs(path);// 一个模拟linux的df命令的一个类,获得SD卡和手机内存的使用情况
long blockSize = stat.getBlockSize();// 返回 Int ,大小,以字节为单位,一个文件系统
long availableBlocks = stat.getAvailableBlocks();// 返回 Int ,获取当前可用的存储空间
availableSpace = availableBlocks * blockSize;
} catch (Exception e) {
LogUtils.e(e);
}
return availableSpace;
}
/** 读取CPU信息文件,获取CPU信息 */
@SuppressWarnings("resource")
private static String getCPUInfos() {
String str1 = "/proc/cpuinfo";
String str2 = "";
StringBuilder result = new StringBuilder();
String resultStr = null;
try {
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
while ((str2 = localBufferedReader.readLine()) != null) {
result.append(str2);
// String cup = str2;
}
if (result != null) {
resultStr = result.toString();
return resultStr;
}
} catch (IOException e) {
LogUtils.e(e);
}
return resultStr;
}
/** 获取android系统版本号 */
public static String getOSVersion() {
String release = android.os.Build.VERSION.RELEASE; // android系统版本号
release = "android" + release;
return release;
}
/** 获得android系统sdk版本号 */
public static String getOSVersionSDK() {
return android.os.Build.VERSION.SDK;
}
/** 获得android系统sdk版本号 */
public static int getOSVersionSDKINT() {
return android.os.Build.VERSION.SDK_INT;
}
/** 获取手机型号 */
public static String getDeviceModel() {
return android.os.Build.MODEL;
}