博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 基本工具类方法及%s妙用
阅读量:7300 次
发布时间:2019-06-30

本文共 4387 字,大约阅读时间需要 14 分钟。

hot3.png

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
 getPhoneInfo(Context context) {    Map
 map = new HashMap
();    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、安装apkpublic 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中添加字符串
Hello,%s!在代码中textView.setText(String.format(getResources().getString(R.string.text),"Android"));输出结果:Hello,Android!

转载于:https://my.oschina.net/u/260921/blog/502276

你可能感兴趣的文章
201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结
查看>>
XAML实例教程系列 - 资源(Resources)
查看>>
LWIP互联网资料汇总
查看>>
外贸术语
查看>>
网络传输流量控制策略小结
查看>>
上传大文件
查看>>
Mybatis面试集合(转)
查看>>
分布式系统的完整介绍(一)
查看>>
考点1
查看>>
Asp.net 程序连接orcle如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,...
查看>>
自己写的模板引擎,模板生成静态页面
查看>>
Android 数据库管理— — —更新数据
查看>>
014_捆绑包与显示模式
查看>>
python : logging模块format类
查看>>
[LeetCode] Two Sum
查看>>
java类中的初始化顺序
查看>>
win10远程桌面连接
查看>>
[转]Web Service与WCF区别
查看>>
vs2010 .net4.0 错误 事件的显式接口实现必须使用事件访问器语法
查看>>
BZOJ1090:[SCOI2003]字符串折叠——题解
查看>>