Android 开发常用接口

android常用接口

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

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

=图片==========================

bitmap转Byte数组(微信分享就需要用到)

public byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(CompressFormat.PNG, 100, output);        if (needRecycle) {
            bmp.recycle();
        }        byte[] result = output.toByteArray();        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }        return result;
    }

Resources转Bitmap

public Bitmap loadBitmap(Resources res, int id) {        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;        InputStream is = res.openRawResource(id);// 获取资源图片
        return BitmapFactory.decodeStream(is, null, opt);
    }

保存图片到SD卡

public void saveBitmapToFile(String url, String filePath) {        File iconFile = new File(filePath);        if (!iconFile.getParentFile().exists()) {
            iconFile.getParentFile().mkdirs();
        }        if (iconFile.exists() && iconFile.length() > 0) {            return;
        }        FileOutputStream fos = null;        InputStream is = null;        try {
            fos = new FileOutputStream(filePath);
            is = new URL(url).openStream();            int data = is.read();            while (data != -1) {
                fos.write(data);
                data = is.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {            try {                if (is != null) {
                    is.close();
                }                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

=系统==============================

#根据包名打开一个应用程序    public boolean openApp(String packageName) {        PackageInfo pi = null;        try {
            pi = mPM.getPackageInfo(packageName, 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace();            return false;
        }        if (pi == null) {            return false;
        }        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(pi.packageName);        List<ResolveInfo> apps = mPM.queryIntentActivities(resolveIntent, 0);        ResolveInfo ri = null;        try {
            ri = apps.iterator().next();
        } catch (Exception e) {            return true;
        }        if (ri != null) {            String tmpPackageName = ri.activityInfo.packageName;            String className = ri.activityInfo.name;            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);            ComponentName cn = new ComponentName(tmpPackageName, className);

            intent.setComponent(cn);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            MarketApplication.getMarketApplicationContext().startActivity(
                    intent);
        } else {            return false;
        }        return true;
    }

判断是否APK是否安装过

public boolean checkApkExist(Context context, String packageName) {        if (packageName == null || "".equals(packageName))            return false;        try {            ApplicationInfo info = context.getPackageManager()
                    .getApplicationInfo(packageName,                            PackageManager.GET_UNINSTALLED_PACKAGES);            return true;
        } catch (NameNotFoundException e) {            return false;
        } catch (NullPointerException e) {            return false;
        }
    }
    ```
#安装APK```java    public void installApk(Context context, String strFileAllName) {        File file = new File(strFileAllName);        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);        String type = "application/vnd.android.package-archive";
        intent.setDataAndType(Uri.fromFile(file), type);
        context.startActivity(intent);
    }

卸载APK

    public void UninstallApk(Context context, String strPackageName) {        Uri packageURI = Uri.parse("package:" + strPackageName);        Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
        context.startActivity(uninstallIntent);
    }

判断SD卡是否可用

    public boolean CheckSD() {        if (android.os.Environment.getExternalStorageState().equals(                android.os.Environment.MEDIA_MOUNTED)) {            return true;
        } else {            return false;
        }
    }

创建快捷方式:

    public void createShortCut(Context contxt) {        // if (isInstallShortcut()) {// 如果已经创建了一次就不会再创建了
        // return;
        // }

        Intent sIntent = new Intent(Intent.ACTION_MAIN);
        sIntent.addCategory(Intent.CATEGORY_LAUNCHER);// 加入action,和category之后,程序卸载的时候才会主动将该快捷方式也卸载
        sIntent.setClass(contxt, Login.class);        Intent installer = new Intent();
        installer.putExtra("duplicate", false);
        installer.putExtra("android.intent.extra.shortcut.INTENT", sIntent);
        installer.putExtra("android.intent.extra.shortcut.NAME", "名字");
        installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE",                Intent.ShortcutIconResource
                        .fromContext(contxt, R.drawable.icon));
        installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        contxt.sendBroadcast(installer);
    }

判断快捷方式是否创建:

private boolean isInstallShortcut() {        boolean isInstallShortcut = false;        final ContentResolver cr = MarketApplication
                .getMarketApplicationContext().getContentResolver();        String AUTHORITY = "com.android.launcher.settings";        Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
                + "/favorites?notify=true");        Cursor c = cr.query(CONTENT_URI,                new String[] { "title", "iconResource" }, "title=?",                new String[] { "名字" }, null);        if (c != null && c.getCount() > 0) {
            isInstallShortcut = true;
        }        if (c != null) {
            c.close();
        }        if (isInstallShortcut) {            return isInstallShortcut;
        }        AUTHORITY = "com.android.launcher2.settings";        CONTENT_URI = Uri.parse("content://" + AUTHORITY
                + "/favorites?notify=true");
        c = cr.query(CONTENT_URI, new String[] { "title", "iconResource" },                "title=?", new String[] { "名字" }, null);        if (c != null && c.getCount() > 0) {
            isInstallShortcut = true;
        }        if (c != null) {
            c.close();
        }        AUTHORITY = "com.baidu.launcher";        CONTENT_URI = Uri.parse("content://" + AUTHORITY
                + "/favorites?notify=true");
        c = cr.query(CONTENT_URI, new String[] { "title", "iconResource" },                "title=?", new String[] { "名字" }, null);        if (c != null && c.getCount() > 0) {
            isInstallShortcut = true;
        }        if (c != null) {
            c.close();
        }        return isInstallShortcut;
    }

过滤特殊字符:

    private String StringFilter(String str) throws PatternSyntaxException {        // 只允许字母和数字
        // String regEx = "[^a-zA-Z0-9]";
        // 清除掉所有特殊字符
        String regEx = "[`~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";        Pattern p = Pattern.compile(regEx);        Matcher m = p.matcher(str);        return m.replaceAll("").trim();
    }

执行shell语句:

    public int execRootCmdSilent(String cmd) {        int result = -1;        DataOutputStream dos = null;        try {            Process p = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(p.getOutputStream());
            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            p.waitFor();
            result = p.exitValue();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {            if (dos != null) {                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }        return result;
    }

获得文件MD5值:

    public String getFileMD5(File file) {        if (!file.isFile()) {            return null;
        }        MessageDigest digest = null;        FileInputStream in = null;        byte buffer[] = new byte[1024];        int len;        try {
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();            return null;
        } finally {            if (in != null) {                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }        BigInteger bigInt = new BigInteger(1, digest.digest());        return bigInt.toString(16);
    }            

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

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

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

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏向治洪

android获取状态栏高度

公司项目中使用了popWindow,并且需求指明了popWindow必须是和Activity类似的全屏模式,并且从页面底部弹出! 当popWindow所有设...

3326
来自专栏编程之路

羊皮书APP(Android版)开发系列(十)Android开发常用工具类

1251
来自专栏上善若水

010android初级篇之Activity的返回值

使用startActivity方式启动的Activity和它的父Activity无关,当它关闭时也不会提供任何反馈。 但是使用startActivityFor...

1834
来自专栏王磊的博客

react-native绑定优酷SDK-附效果图和源码

ReactNative绑定优酷SDK需要用到两部分知识: 优酷本身的sdk绑定; RN与原生界面的交互; 效果: ? RN版本:0.49.3 代码更新日期:20...

3906
来自专栏向治洪

android多线程下载2

在上一集中,我们简单介绍了如何创建多任务下载,但那种还不能拿来实用,这一集我们重点通过代码为大家展示如何创建多线程断点续传下载,这在实际项目中很常用. main...

2189
来自专栏代码GG之家

android常用接口(二)

=图片========================== bitmap转Byte数组(微信分享就需要用到) public byte[] bmpToByteAr...

2288
来自专栏向治洪

RecyclerView 实现横向滚动效果

我相信很久以前,大家在谈横向图片轮播是时候,优先会选择具有HorizontalScrollView效果和ViewPager来做,不过自从Google大会之后,系...

4206
来自专栏极客猴

聊聊Activity那些事

Activity身为四大组件之一,在整个App中扮演着向用户呈现界面的角色。在平常的开发中,我们会自定义一个类去继承Activity去实现界面。而Activit...

732
来自专栏向治洪

android wheelview实现三级城市选择

很早之前看淘宝就有了ios那种的城市选择控件,当时也看到网友有分享,不过那个写的很烂,后来(大概是去年吧),我们公司有这么一个项目,当时用的还是网上比较流行的那...

4296
来自专栏向治洪

android异步操作

为了使UI在数据获取时不僵死,数据获取需要在新开Worker线程中进行,然后将返回结果在UI线程中进行读取并渲染页面。面对这种异步处理,到底如何写才简洁,先后面...

2228

扫码关注云+社区

领取腾讯云代金券