Android Utility class Sharing (get memory/Check network/Screen height/mobile resolution) _android

Source: Internet
Author: User

Copy Code code as follows:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.