android 基本工具类方法及%s妙用

1、获取手机分辨率
public static String getDisplayMetrix(Context context) {
    if (Constant.Screen.SCREEN_WIDTH == 0 || Constant.Screen.SCREEN_HEIGHT == 0) {
        if (context != null) {
            int width = 0;
            int height = 0;
            SharedPreferences DiaplayMetrixInfo = context.getSharedPreferences("display_metrix_info", 0);
            if (context instanceof Activity) {
                WindowManager windowManager = ((Activity) context).getWindowManager();
                Display display = windowManager.getDefaultDisplay();
                DisplayMetrics dm = new DisplayMetrics();
                display.getMetrics(dm);
                width = dm.widthPixels;
                height = dm.heightPixels;

                Editor editor = DiaplayMetrixInfo.edit();
                editor.putInt("width", width);
                editor.putInt("height", height);
                editor.commit();
            } else {
                width = DiaplayMetrixInfo.getInt("width", 0);
                height = DiaplayMetrixInfo.getInt("height", 0);
            }

            Constant.Screen.SCREEN_WIDTH = width;
            Constant.Screen.SCREEN_HEIGHT = height;
        }
    }
    return Constant.Screen.SCREEN_WIDTH + "×" + Constant.Screen.SCREEN_HEIGHT;
}
2、关闭系统的软键盘
public static void dismissSoftKeyboard(Activity activity) {
    View view = activity.getWindow().peekDecorView();
    if (view != null) {
        InputMethodManager inputmanger = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
3、dp—px相互转换
public static int dp2px(Context context, float dpValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
}

public static int px2dp(Context context, float pxValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (pxValue / scale + 0.5f);
}
4、获取设备唯一编码
/**
 * 根据mac地址+deviceid
 * 获取设备唯一编码
 *
 * @return
 */
public static String getDeviceKey() {
    if ("".equals(DEVICEKEY)) {
        String macAddress = "";
        WifiManager wifiMgr = (WifiManager) MainApplication.getIns().getSystemService(MainApplication.WIFI_SERVICE);
        WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
        if (null != info) {
            macAddress = info.getMacAddress();
        }
        TelephonyManager telephonyManager =
                (TelephonyManager) MainApplication.getIns().getSystemService(MainApplication.TELEPHONY_SERVICE);
        String deviceId = telephonyManager.getDeviceId();
        DEVICEKEY = MD5Util.toMD5("android" + Constant.APPKEY + Constant.APPPWD + macAddress + deviceId);
    }
    return DEVICEKEY;
}
5、获取手机及SIM卡相关信息
public static Map<String, String> getPhoneInfo(Context context) {
    Map<String, String> map = new HashMap<String, String>();
    TelephonyManager tm = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    String imei = tm.getDeviceId();
    String imsi = tm.getSubscriberId();
    String phoneMode = android.os.Build.MODEL;
    String phoneSDk = android.os.Build.VERSION.RELEASE;
    map.put("imei", imei);
    map.put("imsi", imsi);
    map.put("phoneMode", phoneMode + "##" + phoneSDk);
    map.put("model", phoneMode);
    map.put("sdk", phoneSDk);
    return map;
}
6、安装apk
public void install(Context context, String fileName) {
    if (TextUtils.isEmpty(fileName) || context == null) {
        return;
    }
    try {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
        context.startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void install(Context context, File file) {
    try {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        context.startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
7、Strings.xml中“%s”的妙用
在strings.xml中添加字符串
<string name="text">Hello,%s!</string>
在代码中
textView.setText(String.format(getResources().getString(R.string.text),"Android"));
输出结果:Hello,Android!
时间: 2024-07-30 04:38:56

android 基本工具类方法及%s妙用的相关文章

Android Handler 异步消息处理机制的妙用 创建强大的图片加载类

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38476887 ,本文出自[张鸿洋的博客] 最近创建了一个群,方便大家交流,群号:55032675 上一篇博客介绍了Android异步消息处理机制,如果你还不了解,可以看:Android 异步消息处理机制 让你深入理解 Looper.Handler.Message三者关系 .那篇博客的最后,提出可以把异步消息处理机制不仅仅是在MainActivity中更新UI,可以用到别的地方,

Android自动化测试工具实现简述

前言 自动化测试在产品开发和测试中都有着非常重要的作用.在产品开发阶段,可靠又重复性地运行以确保没有引进回归问题:在产品测试阶段,可以节省人力成本和时间成本,并能保证更大范围的覆盖.Android自动化测试工具同样可以节省不少人力和时间成本,对其研究和实现对于提高测试效率具有一定的意义.目前市面上也有不少Android自动化测试工具,本人在现有工具的基础上,加上自己的理解和实践向大家介绍自动化测试工具的原理和实现. 了解自动化测试的一定对Robotium并不陌生,Robotium是一款国外的An

10个不可错过的Android 开发工具

今天麦子学院android开发老师给大家介绍android开发人员必须的10个android开发工具: 1.Eclipse ADT Eclipse ADT 是 Eclipse 平台下用来开发 Android 应用程序的插件.   2.The SDK and AVD Manager 该工具包含很多重要的功能,包括管理不同的 Android SDK 版本(构建目标),你知道的 Android 的版本众多,API上有些兼容性问题.另外该工具还用于管理 Android 虚拟设备配置 (AVD) ,用来配

21.Android 时间工具DateUtil

21.Android 时间工具DateUtil Android 时间工具DateUtil String 转换 Date long 转换 String long 去除 时分秒 计算时间差 24小时制转12小时制 Date 转换 String DateUtil全部源码 String 转换 Date /** * String 转换 Date * * @param str * @param format * @return */ public static Date string2Date(String

Android开发实践:Android交叉编译工具链的使用

前面2篇文章分别介绍了Android NDK编译的命令行参数,以及如何在任意目录使用Android.mk来编译本地c/c++代码,Andriod.mk和ndk-build只不过是Android官方提供了一套封装过的Android交叉编译环境而已,其实,你可以不用它,而直接通过传统的Makefile文件来编译你的c/c++代码的,本文即介绍如何直接通过传统的Makefile文件来编译可用于Android平台的库文件. 经常搞嵌入式开发的朋友对于交叉编译环境应该并不陌生,说白了,就是一组运行在x86

Android SQLite3工具的使用

首先要配置的是Android SDK中带有的adb工具: 解压并打开下载好的Android SDK,找到platform-tools文件夹, 右击我的电脑-->属性--->高级系统设置--->环境变量; 建议只在用户变量中设置; 如果有PATH环境变量就双击进行编辑,拷贝找到的platform-tools文件夹的全路径,例如: D:\Program Files\adt-bundle-windows-x86_64-20131030\sdk\platform-tools 粘贴到PATh环境变

Android 时间工具DateUtil

Android 时间工具DateUtil   String 转换 Date long 转换 String long 去除 时分秒 计算时间差 24小时制转12小时制 Date 转换 String DateUtil全部源码 String 转换 Date   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 /** * String 转换 Date * * @param str * @param format * @return */ public static Date st

24.Android 图片工具ImageUtil

24.Android 图片工具ImageUtil Android 图片工具ImageUtil 裁图 Bitmap圆角 缩略图 视频缩略图 各种类型转换 ImageUtil全部源码 裁图 /** * 调用系统自带裁图工具 * * @param activity * @param size * @param uri * @param action * @param cropFile */ public static void cropPicture(Activity activity, int si

【转】Android开发工具--android-studio-bundle-141.2288178

原文网址:http://www.androiddevtools.cn/ AndroidDevTools简介 Android Dev Tools官网地址:www.androiddevtools.cn 收集整理Android开发所需的Android SDK.开发中用到的工具.Android开发教程.Android设计规范,免费的设计素材等. 欢迎大家推荐自己在Android开发过程中用的好用的工具.学习开发教程.用到设计素材,欢迎Star.Fork ?. 如果你对翻译英文的Android开发技术文章