Would like to find out how long the network has been connected for

I am using the following function to get the local IP address of the Android device:

/**
 * Loops through all network interfaces to find one with a connection
 * @return the IP of the connected network device, on error returns null
 */
public String getIPAddress() 
{
    String strIPaddress = null;

    try 
    {
        Enumeration<NetworkInterface> enumNetIF = NetworkInterface.getNetworkInterfaces();
        NetworkInterface netIF = null;

        //loop whilst there are more interfaces
        while(enumNetIF.hasMoreElements()) 
        {
            netIF = enumNetIF.nextElement();

            for (Enumeration<InetAddress> enumIP = netIF.getInetAddresses(); enumIP.hasMoreElements();) 
            {
                InetAddress inetAddress = enumIP.nextElement();

                //check for valid IP, but exclude the loopback address
                if(inetAddress.isLoopbackAddress() != true)
                {
                    strIPaddress = inetAddress.getHostAddress();
                    break;
                }
            }
        }
    } 
    catch (SocketException e) 
    {
        Log.e(TAG, e.getMessage());
    }

    return strIPaddress;
}

However, I also need to know for how long this IP address has been connected for. I've searched through the InetAddress structure and couldn't find it: http://developer.android.com/reference/java/net/InetAddress.html

is there another way to find out for how long the local IP address has existed / connected ?

Answers


AFAIK, you would have to watch for connectivity changes (via ConnectivityManager, CONNECTIVITY_ACTION, and your own BroadcastReceiver) and track it yourself.


You'll need to create a background service to handle the monitoring and record when changes are made to the ConnectivityManager.

Note that you'll want to make sure the service starts at boot (triggered by the intent android.intent.action.BOOT_COMPLETED), too; otherwise, it will only keep track of connection changes after the user launches the service.


Need Your Help

Piping A Grunt task output to another task input?

gruntjs handlebars.js grunt-contrib-htmlmin

I want to minify my Handlebars HTML templates before compiling them. I was wondering is it possible to pipe the output of grunt-contrib-htmlmin as input to grunt-contrib-handlebars without making an

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.