安卓手机相关工具类,很全很有用,附学习笔记+面试整理+进阶书籍
作者:互联网
}
/**
-
获取应用包名
-
@return PackageName
*/
public static String getAppPackageName() {
String _PackageName = “”;
try {
_PackageName = NewsApplication.getApp().getPackageName();
} catch (Exception e) {
MLog.e(“PackageName”, “Exception”, e);
}
return _PackageName;
}
/**
-
获取AndroidId
-
@param pContext
-
@return
*/
public static String getAndroidId() {
String _AndroidID = “”;
try {
try {
_AndroidID = Settings.Secure.getString(NewsApplication.sAppContext.getContentResolver(),Settings.Secure.ANDROID_ID);
} catch (Exception e) {
e.printStackTrace();
return getIMEI();
}
if (!TextUtils.isEmpty(_AndroidID)) {
return _AndroidID;
}else {
return getIMEI();
}
} catch (Exception e) {
e.printStackTrace();
}
return _AndroidID;
}
/**
-
获取APP版本号
-
@param context
-
@return AppVersionName
*/
public static String getAppVersionName() {
String versionName = “”;
try {
PackageManager pm = NewsApplication.sAppContext.getPackageManager();
PackageInfo pi = pm.getPackageInfo(NewsApplication.sAppContext.getPackageName(), 0);
versionName = pi.versionName;
if (versionName == null || versionName.length() <= 0) {
return “”;
}
} catch (Exception e) {
Log.e(“VersionInfo”, “Exception”, e);
}
return versionName;
}
/**
-
获取APP版本号
-
@param context
-
@return AppVersionCode
*/
public static int getAppVersionCode() {
int versionCode = 0;
try {
PackageManager pm = NewsApplication.sAppContext.getPackageManager();
PackageInfo pi = pm.getPackageInfo(NewsApplication.sAppContext.getPackageName(), 0);
versionCode = pi.versionCode;
} catch (Exception e) {
Log.e(“VersionInfo”, “Exception”, e);
}
return versionCode;
}
/**
-
IMEI号,唯一的数字,标识了 GSM 和 UMTS网络里的唯一一个手机
-
-
DeviceId
-
@return
*/
public static String getIMEI() {
return ((TelephonyManager) NewsApplication.sAppContext.getSystemService(Context.TELEPHONY_SERVICE))
.getDeviceId();
}
/**
-
IESI号,唯一的数字,标识了GSM和UMTS 网络里的唯一一个用户
-
@return
*/
public static String getIMSI() {
return ((TelephonyManager) NewsApplication.sAppContext.getSystemService(Context.TELEPHONY_SERVICE))
.getSubscriberId();
}
//获取MCC、MNC
static GetMCCAndMNC mGetMCCAndMNC;
public interface GetMCCAndMNC {
public void onSuccess(String pMCC, String pMNC);
public void onFailure();
}
/**
-
获取MCC、MNC
-
@param pContext
-
@param pMCCAndMNC new GetMCCAndMNC(){};
*/
public static void getMCCAndMNC(Context pContext,GetMCCAndMNC pMCCAndMNC) {
mGetMCCAndMNC = pMCCAndMNC;
String _networkOperator = ((TelephonyManager) pContext.getSystemService(Context.TELEPHONY_SERVICE)).getNetworkOperator();
if (TextUtils.isEmpty(_networkOperator)) {
mGetMCCAndMNC.onFailure();
}else {
if (_networkOperator.length() < 5) {
mGetMCCAndMNC.onFailure();
}else {
String _MCC =_networkOperator.substring(0, 3);
String _MNC = _networkOperator.substring(3);
if (!TextUtils.isEmpty(_MCC) && !TextUtils.is
Empty(_MNC)) {
mGetMCCAndMNC.onSuccess(_MCC, _MNC);
}else{
mGetMCCAndMNC.onFailure();
}
}
}
}
/**
-
手机品牌
-
@return
*/
public static String getBrand() {
return android.os.Build.BRAND;
}
/**
-
手机型号
-
@return
*/
public static String getMobileModel() {
return android.os.Build.MODEL;
}
/**
-
手机号码
-
@return
*/
public static String getMobileNumber() {
return ((TelephonyManager) NewsApplication.sAppContext.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();
}
/**
-
SDK版本
-
@return
*/
public static int getSDKVersionNumber() {
return Build.VERSION.SDK_INT;
}
/**
-
系统版本号
-
@return
*/
public static String getFrimWareVersionNumber() {
return Build.VERSION.RELEASE;
}
/**
-
获取手机CPU核心数
-
@return
*/
public static int getNumCores()
{
// Private Class to display only CPU devices in the directory listing
class CpuFilter implements FileFilter
{
@Override
public boolean accept(File pathname)
{
// Check if filename is “cpu”, followed by a single digit number
if (Pattern.matches(“cpu[0-9]”, pathname.getName()))
{
return true;
}
return false;
}
}
try
{
// Get directory containing CPU info
File dir = new File("/sys/devices/system/cpu/");
// Filter to only list the devices we care about
File[] files = dir.listFiles(new CpuFilter());
// Return the number of cores (virtual CPU devices)
return files.length;
}
catch (Exception e)
{
e.printStackTrace();
// Default to return 1 core
return 1;
}
}
/**
-
获得SDCard总大小
-
@return size (long)
*/
@SuppressWarnings(“deprecation”)
public static long getSDTotalSize() {
if (DeviceInfoUtils.hasSdcard()) {
File _FileExternalStorage = Environment.getExternalStorageDirectory();
StatFs _StatFs = new StatFs(_FileExternalStorage.getPath());
long _BlockSize, _TotalBlocks;
_BlockSize = _StatFs.getBlockSize();
_TotalBlocks = _StatFs.getBlockCount();
return _BlockSize * _TotalBlocks;
}else {
return 0;
}
}
/**
-
获得SDCard剩余容量(可用大小 )
-
@return size (long)
*/
@SuppressWarnings(“deprecation”)
public static long getSDAvailableSize() {
if (DeviceInfoUtils.hasSdcard()) {
File _FileExternalStorage = Environment.getExternalStorageDirectory();
StatFs _StatFs = new StatFs(_FileExternalStorage.getPath());
long _BlockSize, _AvailableBlocks;
_BlockSize = _StatFs.getBlockSize();
_AvailableBlocks = _StatFs.getAvailableBlocks();
return _BlockSize * _AvailableBlocks;
}else {
return 0;
}
}
/**
-
获得机身内存总大小
-
@return size (long)
*/
@SuppressWarnings(“deprecation”)
public long getRomTotalSize() {
File _FileDataDirectory = Environment.getDataDirectory();
StatFs _StatFs = new StatFs(_FileDataDirectory.getPath());
long _BlockSize, _TotalBlocks;
th());
long _BlockSize, _AvailableBlocks;
_BlockSize = _StatFs.getBlockSize();
_AvailableBlocks = _StatFs.getAvailableBlocks();
return _BlockSize * _AvailableBlocks;
}else {
return 0;
}
}
/**
-
获得机身内存总大小
-
@return size (long)
*/
@SuppressWarnings(“deprecation”)
public long getRomTotalSize() {
File _FileDataDirectory = Environment.getDataDirectory();
StatFs _StatFs = new StatFs(_FileDataDirectory.getPath());
long _BlockSize, _TotalBlocks;
标签:return,进阶,StatFs,安卓,long,很全,static,public,String 来源: https://blog.csdn.net/m0_66265052/article/details/122761285