分享

android 一些常用的功能方法代码块

 闹海鱼儿 2012-09-19
开发中常用的代码块:

一 隐藏软键盘的输入法
     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;
        }

        /**
         * 转化KB到MB
         */
        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) {
            string = string.trim();
            if (string.length() != 0) {
                return false;
            }
        }
        return true;
    }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多