其他分享
首页 > 其他分享> > 安卓手机相关工具类,很全很有用,附学习笔记+面试整理+进阶书籍

安卓手机相关工具类,很全很有用,附学习笔记+面试整理+进阶书籍

作者:互联网

}

/**

*/

public static String getAppPackageName() {

String _PackageName = “”;

try {

_PackageName = NewsApplication.getApp().getPackageName();

} catch (Exception e) {

MLog.e(“PackageName”, “Exception”, e);

}

return _PackageName;

}

/**

*/

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;

}

/**

*/

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;

}

/**

*/

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;

}

/**

*/

public static String getIMEI() {

return ((TelephonyManager) NewsApplication.sAppContext.getSystemService(Context.TELEPHONY_SERVICE))

.getDeviceId();

}

/**

*/

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();

}

/**

*/

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();

}

}

}

}

/**

*/

public static String getBrand() {

return android.os.Build.BRAND;

}

/**

*/

public static String getMobileModel() {

return android.os.Build.MODEL;

}

/**

*/

public static String getMobileNumber() {

return ((TelephonyManager) NewsApplication.sAppContext.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();

}

/**

*/

public static int getSDKVersionNumber() {

return Build.VERSION.SDK_INT;

}

/**

*/

public static String getFrimWareVersionNumber() {

return Build.VERSION.RELEASE;

}

/**

*/

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;

}

}

/**

*/

@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;

}

}

/**

*/

@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;

}

}

/**

*/

@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;

}

}

/**

*/

@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