Android – DialogFragment setStyle

Android

//1 -> style = DialogFragment.STYLE_NO_TITLE
//2 -> style = DialogFragment.STYLE_NO_FRAME
//3 -> style = DialogFragment.STYLE_NO_INPUT
//4 -> style = DialogFragment.STYLE_NORMAL
//            --
//4 -> theme = android.R.style.Theme_Holo
//5 -> theme = android.R.style.Theme_Holo_Light_Dialog
//6 -> theme = android.R.style.Theme_Holo_Light
//7 -> theme = android.R.style.Theme_Holo_Light_Panel
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog)

Android – Calculando a intensidade do sinal wifi (Getting WiFi signal strength in Android)

android wifi

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int numberOfLevels = 5;
WifiInfo wifiInfo = wifiManager.getConnectionInfo();Referência
int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);

Referência: https://developer.android.com/reference/android/net/wifi/WifiManager.html#calculateSignalLevel%28int,%20int%29

Android – Desabilitar o recurso multi window programaticamente (Disable multi window feature programatically)

multi-window

Normalmente presente em dispositivos da samsung. Para desabilitar o recurdo multi-window basta executar o seguinte comando:

// requer permissão: android:name="android.permission.WRITE_SETTINGS"
// 0=desabilitado ; 1=habilitado
Settings.System.putInt(contentResolver, "multi_window_enabled", 0)

Android – Esconder o teclado do android programaticamente ‘forçado’ (Hide keyboard programmatically)

keyboard_android

easy workaround:

           InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
           imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);

Java – Ler um arquivo e codifica-lo para Base64 – Converter File para Base64 (Encode file to Base64)

java

 private String encodeFileToBase64Binary(File file)
            throws IOException {

        byte[] bytes = loadFile(file);
        byte[] encoded = Base64.getEncoder().encode(bytes);
        String encodedString = new String(encoded);

        return encodedString;
    }

    private byte[] loadFile(File file) throws IOException {
        byte[] bytes;
        try (InputStream is = new FileInputStream(file)) {
            long length = file.length();
            if (length > Integer.MAX_VALUE) {
                throw new IOException("File to large " + file.getName());
            }
            bytes = new byte[(int) length];
            int offset = 0;
            int numRead = 0;
            while (offset < bytes.length
                    && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                offset += numRead;
            }
            if (offset < bytes.length) {
                throw new IOException("Could not completely read file " + file.getName());
            }
        }
        return bytes;
    }