android实用工具类分享(获取内存/检查网络/屏幕高度/手机分辨率)
public class commonutil {
public static boolean hassdcard() {
string status = environment.getexternalstoragestate();
return status.equals(environment.media_mounted);
}
/**
* 获取最大内存
*
* @return
*/
public static long getmaxmemory() {
return runtime.getruntime().maxmemory() / 1024;
}
/**
* 检查网络
*
* @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;
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(context context, float dpvalue) {
final float scale = context.getresources().getdisplaymetrics().density;
return (int) (dpvalue * scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(context context, float pxvalue) {
final float scale = context.getresources().getdisplaymetrics().density;
return (int) (pxvalue / scale + 0.5f);
}
/**
* 查询手机内非系统应用
*
* @param context
* @return
*/
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;
}
/**
* 获取版本号和版本次数
*
* @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的类名来判断是否启动某个服务
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;
}
}
上一篇: android打开rar压缩文件
下一篇: 铁锅保养小妙招