Practical code 4: practical code

Source: Internet
Author: User

Practical code 4: practical code

1,

/*** Get all the actions in the specified program as Intent. ACTION_MAIN Activity ** @ param context * context object * @ param pkg * package name * @ return specifies that all actions in the program are Intent. activity Set of ACTION_MAIN */public static List <String> getActivities (Context context, String pkg) {List <String> result = new ArrayList <String> (); intent intent = new Intent (Intent. ACTION_MAIN, null); intent. setPackage (pkg); List <ResolveInfo> list = context. getPackageManager (). queryIntentActivities (intent, 0); if (DataUtil. isEmpty (list) {return null;} for (ResolveInfo info: list) {result. add (info. activityInfo. name);} return result ;}

2,

/*** Get the installation path of the specified installed APK (for example:/data/app/com.home.util-2.apk) ** @ param context * Context object * @ param pkg * package name * @ return specifies the APK installation path */public static String getAppSourceDir (context, String pkg) {PackageManager pm = context. getPackageManager (); List <ApplicationInfo> list = pm. getInstalledApplications (0); if (DataUtil. isEmpty (list) {return null;} for (ApplicationInfo appInfo: list) {if (appInfo. packageName. equals (pkg) {return appInfo. sourceDir ;}} return null ;}


3,

/*** Convert a generic set to an array ** @ param cls * class template * @ param list * Source Set * @ return converted array */@ SuppressWarnings ("unchecked") public static <T> T [] listToArray (Class <?> Cls, List <T> list) {if (isEmpty (list) {return (T []) Array. newInstance (cls, 0);} return list. toArray (T []) Array. newInstance (cls, list. size ()));}


4,

/*** Check whether all text is Chinese ** @ param text * @ return returns true if all text is Chinese; otherwise, false */public static boolean isChinese (String text) is returned) {if (isEmpty (text) {return false;} return text. matches ("[\ u4E00-\ u9FA5 \ uF900-\ uFA2D] + ");}


5,

/*** Check whether the text contains Chinese characters ** @ param text * @ return returns true if the text contains Chinese characters. Otherwise, false */public static boolean isHasChinese (String text) is returned) {final String format = "[\ u4E00-\ u9FA5 \ uF900-\ uFA2D]"; boolean result = false; Pattern pattern = Pattern. compile (format); Matcher matcher = pattern. matcher (text); result = matcher. find (); return result ;}


6,

/*** Save the current position of the ListView ** @ param context * Context object * @ param lv * ListView instance **/public static void saveListViewCurPos (context, ListView lv) {int pos = lv. getFirstVisiblePosition (); View v = lv. getChildAt (0); int top = (v = null )? 0: v. getTop (); SharedUtil. saveInt (context, Constant. DEFAULT_SHARE_NAME, "pos", pos); SharedUtil. saveInt (context, Constant. DEFAULT_SHARE_NAME, "top", top );} /*** restore the previous position of the ListView ** @ param context * context object * @ param lv * ListView instance * @ return returns true if the restoration is successful, otherwise, false */public static boolean resumeListViewPos (Context context, ListView lv) {int pos = SharedUtil is returned. getInt (context, Constant. DEFAULT_SHARE_NAME, "pos",-1); int top = SharedUtil. getInt (context, Constant. DEFAULT_SHARE_NAME, "top",-1); if (pos =-1 | top =-1) {return false;} lv. setSelectionFromTop (pos, top); return true ;}


7,

/*** Get the mobile phone IP address ** @ param context * Context object * @ return IP Address */@ SuppressWarnings ("deprecation") public static String getPhoneIp (context) {int ip = getWifiManager (context ). getConnectionInfo (). getIpAddress (); return android. text. format. formatter. formatIpAddress (ip );}


8,

/*** Sort folders (sort folders first and then sort files) ** @ param files * File array */public static void sortFiles (File [] files) {Arrays. sort (files, new Comparator <File> () {@ Overridepublic int compare (File file1, File file2) {boolean l1 = file1.isDirectory (); boolean l2 = file2.isDirectory (); if (l1 &&! L2) return-1; else if (! L1 & l2) return 1; else {return file1.getName (). compareTo (file2.getName ());}}});}



 

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.