Small set of practical methods for Android application development

Source: Internet
Author: User

/** the specified format to return the current system time */
public static string Getdatatime (string format) {
SimpleDateFormat df = new SimpleDateFormat (format);
Return Df.format (New Date ());
}

/** Get phone IMEI (mobile Device International identity Code) Code */
public static String Getphoneimei (context context) {
Telephonymanager TM = (Telephonymanager) context.getsystemservice (Context.telephony_service);
return Tm.getdeviceid ();
}

/** get the Phone System SDK version */
public static int getsdkversion () {
return Android.os.Build.VERSION.SDK_INT;
}

/*** get the system version */
public static String Getsystemversion () {
return Android.os.Build.VERSION.RELEASE;
}

/** Call System Send text message */
public static void Sendsms (context context, String Smsbody) {
Uri Smstouri = Uri.parse ("Smsto:");
Intent Intent = new Intent (intent.action_sendto, Smstouri);
Intent.putextra ("Sms_body", smsbody);
Context.startactivity (Intent);
}

/** determine if the network is connected */
public static Boolean checknet (context context) {
Connectivitymanager cm = (Connectivitymanager) context.getsystemservice (Context.connectivity_service);
Networkinfo info = Cm.getactivenetworkinfo ();
return info! = null;//Determines whether the network is connected based on whether the connection is null
}

/** determine if the network is wifi*/
public static Boolean Iswifi (context context) {
Connectivitymanager cm = (Connectivitymanager) context.getsystemservice (Context.connectivity_service);
WIFI Status: Connectivitymanager.type_wifi
Status of 3G: Connectivitymanager.type_mobile
state state = Cm.getnetworkinfo (Connectivitymanager.type_wifi). GetState ();
return state.connected = = State;
}

/** to determine if the current application is running in the background */
public static Boolean IsBackground (context context) {
Activitymanager Activitymanager = (activitymanager) context.getsystemservice (Context.activity_service);
list<activitymanager.runningappprocessinfo> appprocesses = activitymanager.getrunningappprocesses ();
for (Activitymanager.runningappprocessinfo appprocess:appprocesses) {
if (AppProcess.processName.equals (Context.getpackagename ())) {
if (appprocess.importance = = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
Running in the background
return true;
}
else {
Front desk operation
return false;
}
}
}
return false;
}

/** determine if the phone handles sleep */
public static Boolean issleeping (context context) {
Keyguardmanager kgmgr = (keyguardmanager) context.getsystemservice (Context.keyguard_service);
Boolean issleeping = Kgmgr.inkeyguardrestrictedinputmode ();
return issleeping;
}

/** Install APK */
public static void installapk (context context, file file) {
Intent Intent = new Intent ();
Intent.setaction ("Android.intent.action.VIEW");
Intent.addcategory ("Android.intent.category.DEFAULT");
Intent.settype ("application/vnd.android.package-archive");
Intent.setdata (uri.fromfile (file));
Intent.setdataandtype (uri.fromfile (file), "application/vnd.android.package-archive");
Intent.setflags (Intent.flag_activity_new_task);
Context.startactivity (Intent);
}

/** Analog Click the Home button, run in the background */
public static void GoHome (context context) {
Intent mhomeintent = new Intent (intent.action_main);
Mhomeintent.addcategory (Intent.category_home);
Mhomeintent.addflags (Intent.flag_activity_new_task | intent.flag_activity_reset_task_if_needed);
Context.startactivity (mhomeintent);
}

/** Get app Signature */
public static string Getsign (context context, string pkgname) {
try {
PackageInfo pis = Context.getpackagemanager (). Getpackageinfo (Pkgname, packagemanager.get_signatures);
Return Hexdigest (Pis.signatures[0].tobytearray ());
}
catch (Namenotfoundexception e) {
throw new Kjexception (SystemTool.class.getName () + "the" + Pkgname + "' s application not Found");
}
}

/** Convert the signature string to the required 32-bit signature */
private static String Hexdigest (byte[] paramarrayofbyte) {
Final char[] Hexdigits = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102};
try {
MessageDigest localmessagedigest = messagedigest.getinstance ("MD5");
Localmessagedigest.update (Paramarrayofbyte);
byte[] Arrayofbyte = Localmessagedigest.digest ();
char[] Arrayofchar = new CHAR[32];
for (int i = 0, j = 0;; i++, J + +) {
if (i >=) {return new String (Arrayofchar);}
int k = Arrayofbyte[i];
ARRAYOFCHAR[J] = hexdigits[(0xF & K >>> 4)];
ARRAYOFCHAR[++J] = hexdigits[(K & 0xF)];
}
}
catch (Exception e) {
}
Return "";
}

/** get the available memory size of the device */
public static int Getdeviceusablememory (context context) {
Activitymanager am = (activitymanager) context.getsystemservice (Context.activity_service);
Memoryinfo mi = new Memoryinfo ();
Am.getmemoryinfo (MI);
Returns the available memory for the current system (in units: M)
return (int) (MI.AVAILMEM/(1024 * 1024));
}


/** Cleanup background processes and services */
public static int GC (context context) {
Long i = getdeviceusablememory (context);
int count = 0; Number of processes cleaned out
Activitymanager am = (activitymanager) context.getsystemservice (Context.activity_service);
Get a list of running service
list<runningserviceinfo> servicelist = am.getrunningservices (100);
if (servicelist! = null) for (Runningserviceinfo service:servicelist) {
if (Service.pid = = Android.os.Process.myPid ()) continue;
try {
Android.os.Process.killProcess (SERVICE.PID);
count++;
}
catch (Exception e) {
E.getstacktrace ();
Continue
}
}


Small set of practical methods for Android application development

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.