android实用工具类分享(获取内存/检查网络/屏幕高度/手机分辨率)
代码如下:
public class CommonUtil {
public static boolean hasSDCard() {
String status = Environment.getExternalStorageState();
return status.equals(Environment.MEDIA_MOUNTED);
}
public static long getMaxMemory() {
return Runtime.getRuntime().maxMemory() / 1024;
}
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();
public static int getScreenHeight(Activity context) {
context.getWindowManager().getDefaultDisplay().getMetrics(metric);
return metric.heightPixels;
}
public static int getScreenWidth(Activity context) {
context.getWindowManager().getDefaultDisplay().getMetrics(metric);
return metric.widthPixels;
}
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
public static List<PackageInfo> getAllApps(Context context) {
List<PackageInfo> apps = new ArrayList<PackageInfo>();
PackageManager pManager = context.getPackageManager();
// 获取手机内所有应用
List<PackageInfo> paklist = pManager.getInstalledPackages(0);
for (int i = 0; i < paklist.size(); i++) {
PackageInfo pak = (PackageInfo) paklist.get(i);
// 判断是否为非系统预装的应用程序
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;
}
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的类名来判断是否启动某个服务
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;
}
}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341