android實用工具類分享(獲取內(nèi)存/檢查網(wǎng)絡(luò)/屏幕高度/手機(jī)分辨率)
public class CommonUtil {
public static boolean hasSDCard() {
String status = Environment.getExternalStorageState();
return status.equals(Environment.MEDIA_MOUNTED);
}
/**
* 獲取最大內(nèi)存
*
* @return
*/
public static long getMaxMemory() {
return Runtime.getRuntime().maxMemory() / 1024;
}
/**
* 檢查網(wǎng)絡(luò)
*
* @param context
* @return
*/
public static boolean checkNetState(Context context) {
boolean netstate = false;
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
netstate = true;
break;
}
}
}
}
return netstate;
}
public static void showToast(Context context, String tip) {
Toast.makeText(context, tip, Toast.LENGTH_SHORT).show();
}
public static DisplayMetrics metric = new DisplayMetrics();
/**
* 得到屏幕高度
*
* @param context
* @return
*/
public static int getScreenHeight(Activity context) {
context.getWindowManager().getDefaultDisplay().getMetrics(metric);
return metric.heightPixels;
}
/**
* 得到屏幕寬度
*
* @param context
* @return
*/
public static int getScreenWidth(Activity context) {
context.getWindowManager().getDefaultDisplay().getMetrics(metric);
return metric.widthPixels;
}
/**
* 根據(jù)手機(jī)的分辨率從 dp 的單位 轉(zhuǎn)成為 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根據(jù)手機(jī)的分辨率從 px(像素) 的單位 轉(zhuǎn)成為 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 查詢手機(jī)內(nèi)非系統(tǒng)應(yīng)用
*
* @param context
* @return
*/
public static List<PackageInfo> getAllApps(Context context) {
List<PackageInfo> apps = new ArrayList<PackageInfo>();
PackageManager pManager = context.getPackageManager();
// 獲取手機(jī)內(nèi)所有應(yīng)用
List<PackageInfo> paklist = pManager.getInstalledPackages(0);
for (int i = 0; i < paklist.size(); i++) {
PackageInfo pak = (PackageInfo) paklist.get(i);
// 判斷是否為非系統(tǒng)預(yù)裝的應(yīng)用程序
if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
// customs applications
apps.add(pak);
}
}
return apps;
}
public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) width / w);
float scaleHeight = ((float) height / h);
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
return newbmp;
}
/**
* 獲取版本號和版本次數(shù)
*
* @param context
* @return
*/
public static String getVersionCode(Context context, int type) {
try {
PackageInfo pi = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0);
if (type == 1) {
return String.valueOf(pi.versionCode);
} else {
return pi.versionName;
}
} catch (NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
// 通過Service的類名來判斷是否啟動某個服務(wù)
public static boolean messageServiceIsStart(
List<ActivityManager.RunningServiceInfo> mServiceList,
String className) {
for (int i = 0; i < mServiceList.size(); i++) {
if (className.equals(mServiceList.get(i).service.getClassName())) {
return true;
}
}
return false;
}
}
相關(guān)文章
flutter實現(xiàn)帶刪除動畫的listview功能
最近接了一個新項目,需要開發(fā)帶有刪除動畫效果的listview功能,在實現(xiàn)過程中列表滾動效果用listview實現(xiàn)的,本文通過實例代碼給大家分享實現(xiàn)過程,感興趣的朋友跟隨小編一起學(xué)習(xí)下吧2021-05-05
Android Vibrator調(diào)節(jié)震動代碼實例
這篇文章主要介紹了Android Vibrator調(diào)節(jié)震動代碼實例,本文直接給出實現(xiàn)代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下2015-05-05
Android簽名文件轉(zhuǎn)化為pk8和pem的實現(xiàn)
這篇文章主要介紹了Android簽名文件轉(zhuǎn)化為pk8和pem的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
完美實現(xiàn)ExpandableListView二級分欄效果
這篇文章主要為大家詳細(xì)介紹了ExpandableListView實現(xiàn)二級分欄效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo)
這篇文章主要為大家詳細(xì)介紹了Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05
Android自定義View之酷炫數(shù)字圓環(huán)
這篇文章主要為大家詳細(xì)介紹了Android自定義View之酷炫數(shù)字圓環(huán),實現(xiàn)效果很酷,,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01
Android開發(fā)四大組件之實現(xiàn)電話攔截和電話錄音
這篇文章給大家介紹Android開發(fā)四大組件之實現(xiàn)電話攔截和電話錄音,涉及到android四大基本組件在程序中的應(yīng)用,對android四大基本組件感興趣的朋友可以參考下本篇文章2015-10-10

