android常用接口(一)

时间:2022-04-26
本文章向大家介绍android常用接口(一),主要内容包括dip转px、获取当前窗体,并添加自定义view:、自定义fastScrollBar图片样式:、判断网络是否可用:、判断是不是Wifi连接、判断当前网络类型、获取下载文件的真实名字、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

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;
    }