android常用接口(一)

android常用接口

常见的一些调用接口 更多阅读全文后star,实时更、更新收集到的接口

需要交流,联系微信:code_gg_boy 更多精彩,时时关注微信公众号code_gg_home

dip转px

    public int convertDipOrPx(int dip) {        float scale = MarketApplication.getMarketApplicationContext()
                .getResources().getDisplayMetrics().density;        return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
    }

获取当前窗体,并添加自定义view:

    getWindowManager()
            .addView(
                     overlay,                     new WindowManager.LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.TYPE_APPLICATION,
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                        PixelFormat.TRANSLUCENT));

自定义fastScrollBar图片样式:

        try {
            Field f = AbsListView.class.getDeclaredField("mFastScroller");
            f.setAccessible(true);
            Object o = f.get(listView);
            f = f.getType().getDeclaredField("mThumbDrawable");
            f.setAccessible(true);
            Drawable drawable = (Drawable) f.get(o);
            drawable = getResources().getDrawable(R.drawable.ic_launcher);
            f.set(o, drawable);
            Toast.makeText(this, f.getType().getName(), 1000).show();
        } catch (Exception e) {            throw new RuntimeException(e);
        }

=网络=

判断网络是否可用:

/**
     * 网络是否可用
     * 
     * @param context
     * @return
     */
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] info = mgr.getAllNetworkInfo();        if (info != null) {            for (int i = 0; i < info.length; i++) {                if (info[i].getState() == NetworkInfo.State.CONNECTED) {                    return true;
                }
            }
        }        return false;
    }

方法二:

    /*
     * 判断网络连接是否已开 2012-08-20true 已打开 false 未打开
     */
    public static boolean isConn(Context context) {        boolean bisConnFlag = false;
        ConnectivityManager conManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo network = conManager.getActiveNetworkInfo();        if (network != null) {
            bisConnFlag = conManager.getActiveNetworkInfo().isAvailable();
        }        return bisConnFlag;
    }

判断是不是Wifi连接

    public static boolean isWifiActive(Context icontext) {
        Context context = icontext.getApplicationContext();
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] info;        if (connectivity != null) {
            info = connectivity.getAllNetworkInfo();            if (info != null) {                for (int i = 0; i < info.length; i++) {                    if (info[i].getTypeName().equals("WIFI")
                            && info[i].isConnected()) {                        return true;
                    }
                }
            }
        }        return false;
    }

判断当前网络类型

/**
     * 网络方式检查
     */
    private static int netCheck(Context context) {
        ConnectivityManager conMan = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
                .getState();
        State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                .getState();        if (wifi.equals(State.CONNECTED)) {            return DO_WIFI;
        } else if (mobile.equals(State.CONNECTED)) {            return DO_3G;
        } else {            return NO_CONNECTION;
        }
    }

获取下载文件的真实名字

    public String getReallyFileName(String url) {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork() // 这里可以替换为detectAll()
                                                                      // 就包括了磁盘读写和网络I/O
                .penaltyLog() // 打印logcat,当然也可以定位到dropbox,通过文件保存相应的log
                .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects() // 探测SQLite数据库操作
                .penaltyLog() // 打印logcat
                .penaltyDeath().build());

        String filename = "";
        URL myURL;
        HttpURLConnection conn = null;        if (url == null || url.length() < 1) {            return null;
        }        try {
            myURL = new URL(url);
            conn = (HttpURLConnection) myURL.openConnection();
            conn.connect();
            conn.getResponseCode();
            URL absUrl = conn.getURL();// 获得真实Url
            // 打印输出服务器Header信息
            // Map<String, List<String>> map = conn.getHeaderFields();
            // for (String str : map.keySet()) {
            // if (str != null) {
            // Log.e("H3c", str + map.get(str));
            // }
            // }
            filename = conn.getHeaderField("Content-Disposition");// 通过Content-Disposition获取文件名,这点跟服务器有关,需要灵活变通
            if (filename == null || filename.length() < 1) {
                filename = URLDecoder.decode(absUrl.getFile(), "UTF-8");
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }        return filename;
    }

原文发布于微信公众号 - 代码GG之家(code_gg_home)

原文发表时间:2016-07-02

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

扫码关注云+社区

领取腾讯云代金券