原文地址:http://www.2cto.com/kf/201208/151990.html
我们这些苦逼的程序员在工作中,我们的每一个老板都希望我们都能把手头的工作做好的,而且是越快越好,那我们要怎么样才起来呢?对于常用的代码块无限复做是我们工作中简省时间最有效的途径之一,而下面的这些代码就是我们在开发出现概率较多的,就为大家归纳了一部分开发中常用的代码块:
一 隐藏软键盘的输入法
- InputMethodManager mInputMethodManager = (InputMethodManager) context
- .getSystemService(Context.INPUT_METHOD_SERVICE);
- mInputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
二 判断网络是否是好的
- public static boolean isActiveNetwork(Context context) {
- ConnectivityManager cManager = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo netInfor = cManager.getActiveNetworkInfo();
- if (netInfor != null && netInfor.isAvailable()) {
- return true;
- } else {
- return false;
- }
- }
三 数据单位的转换
- /**
- * 转化B到KB
- */
- public static double transB2KB(long b) {
- return b / 1024;
- }
- /**
- * 转化B到KB
- */
- public static double transKB2M(double KB) {
- return KB / 1024;
- }
四 确保文件目录存在
- public static void checkFileDirectory(String path) {
- if (path != null) {
- File filePath = new File(path);
- if (!filePath.exists()) {
- filePath.mkdirs();
- }
- }
- }
五 获取网络文件的总大小
- public static Long getTotalSize(String url) {
- Long totalSize = null;
- try {
- totalSize = NetworkUtil.getContentSize(url);
- } catch (Exception e) {
- totalSize = 0L;
- e.printStackTrace();
- }
- return totalSize;
- }
六 显示网络异常的提示
- public static void showNetException(Context context) {
- Toast.makeText(context,
- context.getApplicationContext().getResources().getString(R.string.net_exception),
- Toast.LENGTH_SHORT).show();
- }
七 java将天数转换为毫秒数
- public static long transDayToTime(long datCount) {
- long time = datCount * 24 * 60 * 60 * 1000;
- return time;
- }
八 java 将毫秒数转换为天数
- public static int transTimeToDay(long time) {
- int day = (int) (time / (24 * 60 * 60 * 1000));
- return day;
- }
九 android判断应用是否是内置的
- public static boolean isSystemApplication(Context context, String packageName) {
- boolean isflag = false;
- try {
- PackageManager pm = context.getPackageManager();
- ApplicationInfo pInfo = pm
- .getApplicationInfo(packageName, PackageManager.GET_META_DATA);
- if ((pInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
- isflag = true;
- }
- } catch (Exception e) {
- Log.i("xxxxx","Exception ");
- }
- return isflag;
- }
十 判断字符串是否为空
- public static boolean isNull(String string) {
- if (string != null) {
- stringstring = string.trim();
- if (string.length() != 0) {
- return false;
- }
- }
- return true;
- }