public class Commonutil {
public static Boolean Hassdcard () {
String status = Environment.getexternalstoragestate ();
Return Status.equals (environment.media_mounted);
}
/**
* Get maximum memory
*
* @return
*/
public static long Getmaxmemory () {
Return Runtime.getruntime (). MaxMemory ()/1024;
}
/**
* Check the network
*
* @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 ();
/**
* Get screen height
*
* @param context
* @return
*/
public static int Getscreenheight (activity context) {
Context.getwindowmanager (). Getdefaultdisplay (). Getmetrics (Metric);
return metric.heightpixels;
}
/**
* Get screen width
*
* @param context
* @return
*/
public static int Getscreenwidth (activity context) {
Context.getwindowmanager (). Getdefaultdisplay (). Getmetrics (Metric);
return metric.widthpixels;
}
/**
* Change from DP unit to PX (pixel) According to the resolution of the mobile phone
*/
public static int dip2px (context context, float Dpvalue) {
Final float scale = context.getresources (). Getdisplaymetrics (). density;
return (int) (Dpvalue * scale + 0.5f);
}
/**
* According to the resolution of the mobile phone from px (pixel) units to become DP
*/
public static int Px2dip (context context, float Pxvalue) {
Final float scale = context.getresources (). Getdisplaymetrics (). density;
return (int) (Pxvalue/scale + 0.5f);
}
/**
* Search mobile phone for non-system applications
*
* @param context
* @return
*/
public static list<packageinfo> Getallapps (context context) {
List<PackageInfo> apps = new arraylist<packageinfo> ();
packagemanager Pmanager = Context.getpackagemanager ();
//get all the apps in the phone
List<PackageInfo> paklist = pmanager.getinstalledpackages (0);
for (int i = 0; i < paklist.size (); i++) {
packageinfo Pak = (packageinfo) paklist.get (i);
//to determine if a system-preinstalled application
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;
}
/**
* Get version number and version number
*
* @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;
}
}
To determine whether a service is started by the class name of the 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;
}
}