Android's most commonly used quick development tools and android development tools
The tool classes developed by Android can well encapsulate some common operations, which will be very convenient to use in the future. I will share with you the frequently used tool classes.
FileCache:
Package com. pztuan. common. util; import java. io. file; import android. content. context; public class FileCache {private File cacheDir; public FileCache (Context context) {// determine the external storage SD card mounting status. if the mounting is normal, create the SD card cache folder if (android. OS. environment. getExternalStorageState (). equals (android. OS. environment. MEDIA_MOUNTED) {cacheDir = new File (android. OS. environment. getExternalStorageDirectory (), "PztCacheDir");} else {// SD card Mounting Error Frequently, obtain the local cache folder (application package directory) cacheDir = context. getCacheDir ();} if (! CacheDir. exists () {cacheDir. mkdirs () ;}} public File getFile (String url) {String fileName = String. valueOf (url. hashCode (); File file = new File (cacheDir, fileName); return file;} public void clear () {File [] files = cacheDir. listFiles (); for (File f: files) f. delete ();} public String getCacheSize () {long size = 0; if (cacheDir. exists () {File [] files = cacheDir. listFiles (); for (File f: files) {size + = f. length () ;}} String cacheSize = String. valueOf (size/1024/1024) + "M"; return cacheSize ;}}
NetWorkUtil (network type ):
Package com. pztuan. common. util; import android. content. context; import android.net. connectivityManager; import android.net. networkInfo; import android.net. networkInfo. state; import android.net. wifi. wifiManager; import java. security. messageDigest;/***** @ author suncat * @ category network tool */public class NetWorkUtil {private final static String [] hexDigits = {"0", "1 ", "2", "3", "4", "5", "6", "7", "8", "9 ", "A", "B", "c", "d", "e", "f"}; public static final int STATE_DISCONNECT = 0; public static final int STATE_WIFI = 1; public static final int STATE_MOBILE = 2; public static String concatUrlParams () {return null;} public static String encodeUrl () {return null ;} public static boolean isNetWorkConnected (Context context) {ConnectivityManager cm = (ConnectivityManager) context. getSystemService (Context. CONNE CTIVITY_SERVICE); NetworkInfo [] nis = cm. getAllNetworkInfo (); if (nis! = Null) {for (NetworkInfo ni: nis) {if (ni! = Null) {if (ni. isConnected () {return true ;}}} return false;} public static boolean isWifiConnected (Context context) {WifiManager wifiMgr = (WifiManager) context. getSystemService (Context. WIFI_SERVICE); boolean isWifiEnable = wifiMgr. isWifiEnabled (); return isWifiEnable;} public static boolean isNetworkAvailable (Context context) {ConnectivityManager cm = (ConnectivityManager) context. getSystemService (C Ontext. CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm. getActiveNetworkInfo (); if (networkInfo! = Null) {return networkInfo. isAvailable () ;}return false;} private static String byteArrayToHexString (byte [] B) {StringBuffer resultSb = new StringBuffer (); for (int I = 0; I <B. length; I ++) {resultSb. append (byteToHexString (B [I]);} return resultSb. toString ();} private static String byteToHexString (byte B) {int n = B; if (n <0) n = 256 + n; int d1 = n/16; int d2 = n % 16; return hexDigits [d1] + hexDigits [d2];} public static String md5Encode (String origin) {String resultString = null; try {resultString = new String (origin); MessageDigest md = MessageDigest. getInstance ("MD5"); resultString = new String (md. digest (resultString. getBytes ();} catch (Exception ex) {ex. printStackTrace ();} return resultString;} public static String md5EncodeToHexString (String origin) {String resultString = null; try {resultString = new String (origin); MessageDigest md = MessageDigest. getInstance ("MD5"); resultString = byteArrayToHexString (md. digest (resultString. getBytes ();} catch (Exception ex) {ex. printStackTrace ();} return resultString;} public static int getNetworkState (Context context) {ConnectivityManager connManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); // WifiState state = connManager. getNetworkInfo (ConnectivityManager. TYPE_WIFI ). getState (); if (state = State. CONNECTED | state = State. CONNECTING) {return STATE_WIFI;} // 3 Gstate = connManager. getNetworkInfo (ConnectivityManager. TYPE_MOBILE ). getState (); if (state = State. CONNECTED | state = State. CONNECTING) {return STATE_MOBILE;} return STATE_DISCONNECT ;}}
Tools (common minor functions: regular number matching, date calculation, imei number retrieval, and listview height calculation ):
Package com. pztuan. common. util; import java. security. messageDigest; import java. text. parseException; import java. text. simpleDateFormat; import java. util. date; import java. util. regex. matcher; import java. util. regex. pattern; import android. annotation. suppressLint; import android. content. context; import android. OS. environment; import android. telephony. telephonyManager; import android. view. view; import android. vie W. viewGroup; import android. widget. listAdapter; import android. widget. listView; import android. widget. toast; @ SuppressLint ("DefaultLocale") public class Tools {private final static String [] hexDigits = {"0", "1", "2", "3 ", "4", "5", "6", "7", "8", "9", "a", "B", "c", "d ", "e", "f"}; public static String byteArrayToHexString (byte [] B) {StringBuffer resultSb = new StringBuffer (); for (int I = 0; I <B. len Resume; I ++) {resultSb. append (byteToHexString (B [I]);} return resultSb. toString ();} private static String byteToHexString (byte B) {int n = B; if (n <0) n = 256 + n; int d1 = n/16; int d2 = n % 16; return hexDigits [d1] + hexDigits [d2];} /*** md5 encryption ** @ param origin * @ return */public static String md5Encode (String origin) {String resultString = null; try {resultString = new String (origin ); messageDigest md = MessageDigest. getInstance ("MD5"); resultString = byteArrayToHexString (md. digest (resultString. getBytes ();} catch (Exception ex) {ex. printStackTrace ();} return resultString;}/*** Mobile Phone Number Format match ** @ param mobiles * @ return */public static boolean isMobileNO (String mobiles) {Pattern p = Pattern. compile ("^ (13 [0-9]) | (15 [^ 4, \ D]) | (18 [,-9]) \ d {8} $ "); Matcher m = p. matcher (mobiles); System. out. println (M. matches () + "-telnum-"); return m. matches ();}/*** whether the specified character is contained ** @ param expression * @ param text * @ return */private static boolean matchingText (String expression, String text) {Pattern p = Pattern. compile (expression); Matcher m = p. matcher (text); boolean B = m. matches (); return B;}/*** zip code ** @ param zipcode * @ return */public static boolean isZipcode (String zipcode) {Pattern p = Pattern. compile ("[0-9] \ d {5}"); Matcher m = p. matcher (zipcode); System. out. println (m. matches () + "-zipcode-"); return m. matches ();}/*** email format ** @ param email * @ return */public static boolean isValidEmail (String email) {Pattern p = Pattern. compile ("^ ([a-z0-9A-Z] + [-| \.]?) + [A-z0-9A-Z] @ ([a-z0-9A-Z] + (-[a-z0-9A-Z] + )? \\.) + [A-zA-Z] {2,} $ "); Matcher m = p. matcher (email); System. out. println (m. matches () + "-email-"); return m. matches ();}/*** fixed-line number format ** @ param telfix * @ return */public static boolean isTelfix (String telfix) {Pattern p = Pattern. compile ("d {3}-d {8} | d {4}-d {7}"); Matcher m = p. matcher (telfix); System. out. println (m. matches () + "-telfix-"); return m. matches ();}/*** username match ** @ param name * @ return */public stat Ic boolean isCorrectUserName (String name) {Pattern p = Pattern. compile ("([A-Za-z0-9]) {}"); Matcher m = p. matcher (name); System. out. println (m. matches () + "-name-"); return m. matches ();}/*** Password Match. It must start with a letter and be between 6 and 18 characters. It can only contain characters, numbers, and underscores. ** @ Param pwd * @ return **/public static boolean isCorrectUserPwd (String pwd) {Pattern p = Pattern. compile ("\ w {6, 18}"); Matcher m = p. matcher (pwd); System. out. println (m. matches () + "-pwd-"); return m. matches ();}/*** check whether SDCard exists ** @ return */public static boolean hasSdcard () {String state = Environment. getExternalStorageState (); if (state. equals (Environment. MEDIA_MOUNTED) {return true;} else {return false;}/*** calculate the remaining date ** @ param remainTime * @ return */public static String calculationRemainTime (String endTime, long countDown) {SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); try {Date now = new Date (System. currentTimeMillis (); // obtain the current time Date endData = df. parse (endTime); long l = endData. getTime ()-countDown-now. getTime (); long day = l/(24*60*60*1000); long hour = (l/(60*60*1000)-day * 24 ); long min = (l/(60*1000)-day * 24*60-hour * 60 ); long s = (l/1000-day * 24*60*60-hour * 60*60-min * 60 ); return "remaining" + day + "day" + hour + "hour" + min + "Minute" + s + "second";} catch (ParseException e) {e. printStackTrace ();} return "";} public static void showLongToast (Context act, String pMsg) {Toast toast = Toast. makeText (act, pMsg, Toast. LENGTH_LONG); toast. show ();} public static void show1_toast (Context act, String pMsg) {Toast toast = Toast. makeText (act, pMsg, Toast. LENGTH_SHORT); toast. show ();}/*** get the Imei number of the mobile phone ** @ param context * @ return */public static String getImeiCode (Context context) {TelephonyManager tm = (TelephonyManager) context. getSystemService (Context. TELEPHONY_SERVICE); return tm. getDeviceId ();}/*** @ author sunglasses * @ param listView * @ category calculate the height of the listview */public static void setListViewHeightBasedOnChildren (ListView listView) {ListAdapter listAdapter = listView. getAdapter (); if (listAdapter = null) {// pre-conditionreturn;} int totalHeight = 0; for (int I = 0; I <listAdapter. getCount (); I ++) {View listItem = listAdapter. getView (I, null, listView); listItem. measure (0, 0); totalHeight + = listItem. getMeasuredHeight ();} ViewGroup. layoutParams params = listView. getLayoutParams (); params. height = totalHeight + (listView. getDividerHeight () * (listAdapter. getCount ()-1); listView. setLayoutParams (params );}}
SharedPreferencesUtil:
package com.pztuan.db;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.util.Log;import java.util.ArrayList;import java.util.Set;public class SharedPreferencesUtil {private static final String TAG = "PZTuan.SharePreferencesUtil";private static final String SHAREDPREFERENCE_NAME = "sharedpreferences_pztuan";private static SharedPreferencesUtil mInstance;private static SharedPreferences mSharedPreferences;private static SharedPreferences.Editor mEditor;public synchronized static SharedPreferencesUtil getInstance(Context context) {if (mInstance == null) {mInstance = new SharedPreferencesUtil(context);}return mInstance;}private SharedPreferencesUtil(Context context) {mSharedPreferences = context.getSharedPreferences(SHAREDPREFERENCE_NAME, Context.MODE_PRIVATE);mEditor = mSharedPreferences.edit();}public synchronized boolean putString(String key, String value) {mEditor.putString(key, value);return mEditor.commit();}public synchronized boolean putStringArrayList(String key,ArrayList<String> value) {for (int j = 0; j < value.size() - 1; j++) {if (value.get(value.size() - 1).equals(value.get(j))) {value.remove(j);}}mEditor.putInt("citySize", value.size());if (value.size() == 4) {mEditor.putString(key + 0, value.get(3));mEditor.putString(key + 1, value.get(0));mEditor.putString(key + 2, value.get(1));} else if (value.size() == 3) {mEditor.putString(key + 0, value.get(2));mEditor.putString(key + 1, value.get(0));mEditor.putString(key + 2, value.get(1));} else {for (int i = 0; i < value.size(); i++) {mEditor.putString(key + i, value.get(value.size() - 1 - i));}}return mEditor.commit();}public synchronized boolean putInt(String key, int value) {mEditor.putInt(key, value);return mEditor.commit();}public synchronized boolean putLong(String key, long value) {mEditor.putLong(key, value);return mEditor.commit();}public synchronized boolean putFloat(String key, float value) {mEditor.putFloat(key, value);return mEditor.commit();}public synchronized boolean putBoolean(String key, boolean value) {mEditor.putBoolean(key, value);return mEditor.commit();}public synchronized boolean putStringSet(String key, Set<String> value) {mEditor.putStringSet(key, value);return mEditor.commit();}public String getString(String key, String value) {return mSharedPreferences.getString(key, value);}public ArrayList<String> getStringArrayList(String key, int size) {ArrayList<String> al = new ArrayList<String>();int loop;if (size > 4)loop = 4;elseloop = size;for (int i = 0; i < loop; i++) {String name = mSharedPreferences.getString(key + i, null);al.add(name);}return al;}public int getInt(String key, int value) {return mSharedPreferences.getInt(key, value);}public long getLong(String key, long value) {return mSharedPreferences.getLong(key, value);}public float getFloat(String key, float value) {return mSharedPreferences.getFloat(key, value);}public boolean getBoolean(String key, boolean value) {return mSharedPreferences.getBoolean(key, value);}public Set<String> getStringSet(String key, Set<String> value) {return mSharedPreferences.getStringSet(key, value);}public boolean remove(String key) {mEditor.remove(key);return mEditor.commit();}private static final String PREFERENCES_AUTO_LOGIN = "yyUserAutoLogin";private static final String PREFERENCES_USER_NAME = "yyUserName";private static final String PREFERENCES_USER_PASSWORD = "yyUserPassword";public boolean isAutoLogin() {return mSharedPreferences.getBoolean(PREFERENCES_AUTO_LOGIN, false);}public String getUserName() {return mSharedPreferences.getString(PREFERENCES_USER_NAME, "");}public String getUserPwd() {return mSharedPreferences.getString(PREFERENCES_USER_PASSWORD, "");}public void saveLoginInfo(Boolean autoLogin, String userName,String userPassword) {assert (mEditor != null);mEditor.putBoolean(PREFERENCES_AUTO_LOGIN, autoLogin);mEditor.putString(PREFERENCES_USER_NAME, userName);mEditor.putString(PREFERENCES_USER_PASSWORD, userPassword);mEditor.commit();}public void saveLoginPassword(String userPassword) {mEditor.putString(PREFERENCES_USER_PASSWORD, userPassword);mEditor.commit();}public void saveLoginUserid(String userid) {mEditor.putString("userid", userid);mEditor.commit();}public void clearUserInfo() {assert (mEditor != null);mEditor.putBoolean(PREFERENCES_AUTO_LOGIN, false);mEditor.putString(PREFERENCES_USER_NAME, "");mEditor.putString(PREFERENCES_USER_PASSWORD, "");mEditor.putString("userid", "");mEditor.commit();}}
If you have any questions, please leave a message and repeat the source.
Android development requires development tools
JDK1.6 or above
Eclipse + ADT plug-in
The file size of the andoid SDK depends on the ins from 1 GB to 7 GB.
You can use PS and other tools if the interface is beautiful.
In software development, what tools are commonly used in java?
You can look at the api, such as the file class for saving files, and document builer for reading xml.