今後會多收集項目中常用的小功能,提高以後的開發效率,好記星不如爛筆頭,好好寫部落格,好好學習。
1.驗證EditText
/** * <判斷EditText是否為空白> * @param edText * @return * @see [類、類#方法、類#成員] */ public static boolean isEmptyEditText(EditText edText) { if (edText.getText().toString().trim().length() > 0) return false; else return true; }/** * <驗證郵箱是否合法> * @param email * @return * @see [類、類#方法、類#成員] */ public static boolean isEmailIdValid(String email) { String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$"; CharSequence inputStr = email; Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(inputStr); if (matcher.matches()) return true; else return false; }
2.檢查網路連接狀態
/** * 檢查網路連接狀態 * * @param context * @return true or false */ public static boolean isNetworkAvailable(Context context) { ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { return true; } return false; }
3.擷取應用表徵圖
/** * <擷取當前應用的表徵圖> * @param mContext * @return * @see [類、類#方法、類#成員] */ public static Drawable getAppIcon(Context mContext) { Drawable icon = null; final PackageManager pm = mContext.getPackageManager(); String packageName = mContext.getPackageName(); try { icon = pm.getApplicationIcon(packageName); return icon; } catch (NameNotFoundException e1) { e1.printStackTrace(); } return null; }
4.發送本地通知
/** * 發送本地通知 * * @param mContext 上下文 * @param 通知的標題 * @param 通知的內容 * @param 點擊通知要開啟的Intent */ @SuppressLint("NewApi") @SuppressWarnings({"static-access"}) public static void sendLocatNotification(Context mContext, String title, String message, Intent mIntent) { int appIconResId = 0; PendingIntent pIntent = null; if (mIntent != null) pIntent = PendingIntent.getActivity(mContext, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT); final PackageManager pm = mContext.getPackageManager(); String packageName = mContext.getPackageName(); ApplicationInfo applicationInfo; try { applicationInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA); appIconResId = applicationInfo.icon; } catch (NameNotFoundException e1) { e1.printStackTrace(); } // Notification notification = new Notification.Builder(mContext) // .setSmallIcon(appIconResId).setWhen(System.currentTimeMillis()) // .setContentTitle(title).setContentText(message) // .setContentIntent(pIntent).getNotification(); Notification notification; if (mIntent == null) { notification = new Notification.Builder(mContext).setSmallIcon(appIconResId) .setWhen(System.currentTimeMillis()) .setContentTitle(message) .setStyle(new Notification.BigTextStyle().bigText(message)) .setAutoCancel(true) .setContentText(message) .setContentIntent(PendingIntent.getActivity(mContext, 0, new Intent(), 0)) .getNotification(); } else { notification = new Notification.Builder(mContext).setSmallIcon(appIconResId) .setWhen(System.currentTimeMillis()) .setContentTitle(message) .setContentText(message) .setAutoCancel(true) .setStyle(new Notification.BigTextStyle().bigText(message)) .setContentIntent(pIntent) .getNotification(); } // 點擊之後清除通知 notification.flags |= Notification.FLAG_AUTO_CANCEL; // 開啟通知提示音 notification.defaults |= Notification.DEFAULT_SOUND; // 開啟震動 notification.defaults |= Notification.DEFAULT_VIBRATE; NotificationManager manager = (NotificationManager)mContext.getSystemService(mContext.NOTIFICATION_SERVICE); // manager.notify(0, notification); manager.notify(R.string.app_name, notification); }
5.隨機擷取
/** * <隨機擷取字母a-z> * @return random num * @see [類、類#方法、類#成員] */ public static char getRandomCharacter() { Random r = new Random(); char c = (char)(r.nextInt(26) + 'a'); return c; } /** * <擷取0到number之間的隨機數> * @param number * @return * @see [類、類#方法、類#成員] */ public static int getRandom(int number) { Random rand = new Random(); return rand.nextInt(number); }
6.開啟日期和時間選取器
private static Calendar dateTime = Calendar.getInstance(); /** * 開啟日期選取器 * * @param mContext * @param format * @param mTextView 要顯示的TextView */ public static void showDatePickerDialog(final Context mContext, final String format, final TextView mTextView) { new DatePickerDialog(mContext, new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { SimpleDateFormat dateFormatter = new SimpleDateFormat(format); dateTime.set(year, monthOfYear, dayOfMonth); mTextView.setText(dateFormatter.format(dateTime.getTime()).toString()); } }, dateTime.get(Calendar.YEAR), dateTime.get(Calendar.MONTH), dateTime.get(Calendar.DAY_OF_MONTH)).show(); } /** * 時間選取器 * * @param mContext * @param mTextView 要顯示的TextView * @return show timepicker */ public static void showTimePickerDialog(final Context mContext, final TextView mTextView) { new TimePickerDialog(mContext, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { SimpleDateFormat timeFormatter = new SimpleDateFormat("hh:mm a"); dateTime.set(Calendar.HOUR_OF_DAY, hourOfDay); dateTime.set(Calendar.MINUTE, minute); mTextView.setText(timeFormatter.format(dateTime.getTime()).toString()); } }, dateTime.get(Calendar.HOUR_OF_DAY), dateTime.get(Calendar.MINUTE), false).show(); }
7.擷取裝置寬高
/** * 擷取裝置高度 * * @param mContext * @return 裝置高度 */ public static int getDeviceHeight(Context mContext) { DisplayMetrics displaymetrics = new DisplayMetrics(); ((Activity)mContext).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); return displaymetrics.heightPixels; } /** * 擷取裝置寬度 * * @param mContext * @return 裝置寬度 */ public static int getDeviceWidth(Context mContext) { DisplayMetrics displaymetrics = new DisplayMetrics(); ((Activity)mContext).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); return displaymetrics.widthPixels; }
8.String和List互轉
/** * <String轉化為List> * @param string * @return * @see [類、類#方法、類#成員] */ public static List<String> stringToArrayList(String string) { List<String> strValueList = new ArrayList<String>(); strValueList = Arrays.asList(string.split(",")); return strValueList; } /** * <List轉化為String> * @param string * @return * @see [類、類#方法、類#成員] */ public static String arrayListToString(List<String> list) { String strValue = null; StringBuilder sb = new StringBuilder(); for (String s : list) { sb.append(s + ","); strValue = sb.toString(); } if (strValue.length() > 0 && strValue.charAt(strValue.length() - 1) == ',') { strValue = strValue.substring(0, strValue.length() - 1); } return strValue; }
9.Bitmap和Drawable互轉
/** * * <drawable轉bitmap> * @param mContext * @param drawable * @return * @see [類、類#方法、類#成員] */ public static Bitmap drawableTobitmap(Context mContext, int drawable) { Drawable myDrawable = mContext.getResources().getDrawable(drawable); return ((BitmapDrawable)myDrawable).getBitmap(); } /** * * <bitmap轉drawable> * @param mContext * @param drawable * @return * @see [類、類#方法、類#成員] */ public static Drawable bitmapToDrawable(Context mContext, Bitmap bitmap) { return new BitmapDrawable(bitmap); }
10.擷取應用版本號碼
/** * <擷取應用版本號碼> * @param mContext * @return * @see [類、類#方法、類#成員] */ public static int getAppVersionCode(Context mContext) { PackageInfo pInfo = null; try { pInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0); } catch (NameNotFoundException e) { e.printStackTrace(); } return pInfo.versionCode; }