我爱编程

开发过程中的灵光闪动

2018-07-25  本文已影响42人  孤诣

记录开发过程中遇到的好方法,想到的怪方法,奇方法以及奇思路,不断更

关于资源

Kotlin

/**
 * Android中读取assets目录下文件的方法,返回字符串
 */
fun AssetManager.readFile(fileName: String): String {
    open(fileName).use {
        return it.readBytes().toString(Charset.defaultCharset())
    }
}

Android

import android.text.format.Formatter;
long fileLen = 1024L;
String fileSize = Formatter.formatFileSize(context, fileLen);
则 fileSize = 1.00KB
/**
 * 判断手机是触屏手机还是按键手机
 * @return true 按键手机; false 触屏手机
 */
public boolean isHardwareAndroid() {
    int[] deviceIds = InputDevice.getDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice inputDevice = InputDevice.getDevice(deviceId);
        boolean[] booleans = inputDevice.hasKeys(KeyEvent.KEYCODE_DPAD_CENTER, KeyEvent.KEYCODE_CALL);
        if (booleans[0] && booleans[1]) {
            return true;
        }
    }
    return false;
}
/**
 * 判断手机是触屏手机还是按键手机
 * @return true 按键手机; false 触屏手机
 */
public static boolean isHardwareAndroid2() {
        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER);
        InputDevice inputDevice = keyEvent.getDevice();
        boolean[] booleans = inputDevice.hasKeys(KeyEvent.KEYCODE_DPAD_CENTER, KeyEvent.KEYCODE_CALL);
        return booleans[0] && booleans[1];
    }
上一篇 下一篇

猜你喜欢

热点阅读