Common development tools in Android-continuous update...
1. Custom ActionBar
Public class ActionBarTool {public static void setActionBarLayout (Activity act, Context context, int layoutId) {// The first two parameters can generally be set to this, the third parameter is the Id ActionBar actionBar = act of the custom View. getActionBar (); if (null! = ActionBar) {actionBar. setDisplayShowHomeEnabled (false); // whether the application icon actionBar in the upper left corner is displayed. setDisplayShowCustomEnabled (true); // whether to use the custom title bar LayoutInflater inflator = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); View v = inflator. inflate (layoutId, null); // fill in the custom Layout ActionBar. layoutParams layout = new ActionBar. layoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT); actionBar. setCustomView (v, layout); // set the custom View to ActionBar }}}
Ii. system information tools
Public class SystemInfoUtils {public static int getRunningAppProcesses (Context context) {// obtain the number of app processes running in the current system ActivityManager am = (ActivityManager) context. getSystemService (Context. ACTIVITY_SERVICE); List
AppInfos = am. getRunningAppProcesses (); return appInfos. size ();} public static List
> GetAppInfo (Context context) {// obtain the app information List of a non-System Application
> AppInfos = new ArrayList
> (); HashMap
Map = null; List
Packages = context. getPackageManager (). getInstalledPackages (0); // The value 0 indicates the filtering permission. Some mobile phones may not allow you to obtain application information for (PackageInfo temp: packages) {if (temp. applicationInfo. flags & temp. applicationInfo. FLAG_SYSTEM) = 0) {// non-system application // obtain the application name String appName = temp. applicationInfo. loadLabel (context. getPackageManager ()). toString (); // obtain the application icon Drawable appIcon = temp. applicationInfo. loadIcon (context. getPackageManager (); // get the last update time of the Application long lastUpdateTime = temp. lastUpdateTime; // obtain the application package name String packageName = temp. packageName; // obtain the application version String versionName = temp. versionName; map = new HashMap
(); Map. put (appName, appName); map. put (appIcon, appIcon); map. put (lastUpdateTime, lastUpdateTime); map. put (packageName, packageName); map. put (versionName, versionName); appInfos. add (map); // add application information to the set and return map = null;} else {// System Application} return appInfos ;} // get the SD card capacity public static String getSdSize (Context context) {String totalStr = null, availStr = null; if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {// determines whether the SD card File path = Environment is detected. getExternalStorageDirectory (); StatFs stat = new StatFs (path. getPath (); long blockSize = stat. getBlockSizeLong (); long availableBlocks = stat. getAvailableBlocksLong (); long totalBlocks = stat. getBlockCountLong (); long totalSize = blockSize * totalBlocks; long availSize = blockSize * availableBlocks; totalStr = Formatter. formatFileSize (context, totalSize); availStr = Formatter. formatFileSize (context, availSize);} elseToast. makeText (context, SD card not detected, please check if it is inserted correctly, Toast. LENGTH_SHORT ). show (); return total SD card capacity: + totalStr + available: + availStr;} // get the mobile phone memory public static String getRomSave (Context context) {String totalStr = null, availStr = null; File path = Environment. getDataDirectory (); StatFs stat = new StatFs (path. getPath (); // obtain the cell phone memory path long blockSize = stat. getBlockSizeLong (); long availableBlocks = stat. getAvailableBlocksLong (); long totalBlocks = stat. getBlockCountLong (); long totalSize = blockSize * totalBlocks; long availSize = blockSize * availableBlocks; totalStr = Formatter. formatFileSize (context, totalSize); availStr = Formatter. formatFileSize (context, availSize); return total phone memory size: + totalStr + available space: + availStr ;}}
Iii. Network-related tools
Public class NetworkUtils {// determine whether the current network is available public static boolean networkIsConnect (Context context) {ConnectivityManager conn = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo info = conn. getActiveNetworkInfo (); if (info! = Null & info. isConnected () {return true;} else {return false ;}/// determine whether the current connected network is wifi, obtain the public static boolean networkIsWifi (Context context) {ConnectivityManager conn = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo info = conn. getActiveNetworkInfo (); if (info! = Null & info. isConnected () & info. getType () = ConnectivityManager. TYPE_WIFI) {WifiManager wifiManager = (WifiManager) context. getSystemService (Context. WIFI_SERVICE); WifiInfo wifiInfo = wifiManager. getConnectionInfo (); Toast. makeText (context, the id of the connected Wi-Fi network is: + wifiInfo. getNetworkId (), Toast. LENGTH_SHORT ). show (); return true;} else {return false ;}// whether to enable Wifipublic static void setWifiEnabled (Context context, boolean enabled) {WifiManager wifiManager = (WifiManager) context. getSystemService (Context. WIFI_SERVICE); if (enabled) {wifiManager. setWifiEnabled (true);} else {wifiManager. setWifiEnabled (false );}}}
Iv. Tools for Px and Dp Conversion
public class DensityUtils {public static int Dp2Px(Context context, float dp) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dp * scale + 0.5f); } public static int Px2Dp(Context context, float px) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (px / scale + 0.5f); } }
5. Vibration Tool
Public class VibratorUtil {/*** long milliseconds: the duration of the vibration, in milliseconds * long [] pattern: Custom vibration mode. The meanings of numbers in the array are [static duration, vibration duration, static duration, and vibration duration]. The unit of duration is millisecond * boolean isRepeat: whether to Vibrate repeatedly. If it is true, it vibrates repeatedly. If it is false, it only shakes once */public static void Vibrate (final Context context) {long milliseconds = 100; Vibrator vib = (Vibrator) context. getSystemService (Service. VIBRATOR_SERVICE); vib. vibrate (milliseconds);} public static void Vibrate (final Context context, long milliseconds) {Vibrator vib = (Vibrator) context. getSystemService (Service. VIBRATOR_SERVICE ); Vib. vibrate (milliseconds);} public static void Vibrate (final Context context, long [] pattern, boolean isRepeat) {Vibrator vib = (Vibrator) context. getSystemService (Service. VIBRATOR_SERVICE); vib. vibrate (pattern, isRepeat? 1:-1); //-1 indicates repeated vibrations }}