Translate

Monday, September 26, 2016

Android: Get a List of Local Address in Device

This code snippet basically collects an array of Local Addresses (Inet4Address).

We use instaceof Inet4Address to avoid duplicates with IPv4 ang IPv6.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public static Inet4Address[] getLocalAddrasses(ConnectivityManager connManager) throws SocketException {
        ArrayList<Inet4Address> results = new ArrayList<Inet4Address>(1);
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface iface = interfaces.nextElement();
            if (!iface.isUp() || iface.isLoopback() || !iface.supportsMulticast()) {
                continue;
            }
            Enumeration<InetAddress> addresses = iface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress addr = addresses.nextElement();
                if (addr instanceof Inet4Address) {
                    results.add((Inet4Address) addr);
                }
            }
        }
        return results.toArray(new Inet4Address[results.size()]);
    }

Android: Restrict taking screenshots!

Somehow you wanted to block taking a snapshot on a certain application to disallow users share critical information(s).

I've seen this feature in some Online Banking and Finance apps. I got a little curious about it. So here is a simple code snippet;

1
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
Note: Make sure you call this before the setContentView inside onCreate.