如何计算wifi连接时间......?

时间:2015-03-18 03:10:12

标签: android

我正在创建一个应用程序,其中我想计算wifi连接持续时间...为此我使用广播接收器来获得连接和断开状态..........但我不是得到结果..

ConnectivityManager conman = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = conman.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            State state = info.getState();          

            SharedPreferences prefs = getActivity().getPreferences(0);
            Editor edit = prefs.edit();           

            if(state == NetworkInfo.State.CONNECTED)
            {   

                int hour,minute,sec;
                Time now  = new Time();
                now.setToNow();

                hour = now.hour;
                minute = now.minute;
                sec = now.second;

                String conTime = hour+":"+minute+":"+sec;

                edit.putString("conTime", conTime);
                edit.commit();
                connTime.setText(conTime);
            }
            else if(state == NetworkInfo.State.DISCONNECTED )
            {   
                int hourd,mind,secd;

                Time nowd = new Time();
                nowd.setToNow();

                hourd = nowd.hour;
                mind = nowd.minute;
                secd = nowd.second;

                String dconTime = hourd+":"+mind+":"+secd;
                edit.putString("dconTime", dconTime);
                edit.commit();
                disconnTime.setText(dconTime);

            }

为此我首先在我的应用程序中使用了两个文本视图来显示连接wifi的时间...第二个显示断开连接的时间....但每次应用程序启动时都会显示已连接的时间...但我不希望我希望两个textview保持空白....直到我打开/关闭wifi ...我怎么能这样做....

1 个答案:

答案 0 :(得分:2)

你可以通过这种方式检索连接上的系统时间,当它断开连接时你可以得到那个时间实例,通过减去它会给你totalTime,这可以用来获取Calender实例,你可以从中轻松获得时/分/秒。

if(state == NetworkInfo.State.CONNECTED){
    long connectedTime = System.currentTimeMillis();
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(connectedTime);
    yourTextView.setText(cal.get(Calendar.HOUR_OF_DAY) + " hours, "+ cal.get(Calendar.MINUTE) + " minutes, "+ cal.get(Calendar.SECOND) + " seconds ");

}
else if(state == NetworkInfo.State.DISCONNECTED ){
    long disconnectedTime = System.currentTimeMillis();
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(disconnectedTime);
    yourTextView.setText(cal.get(Calendar.HOUR_OF_DAY) + " hours, "+ cal.get(Calendar.MINUTE) + " minutes, "+ cal.get(Calendar.SECOND) + " seconds ");
}

// long totalDuration = disconnectedTime - connectedTime;

if(state == NetworkInfo.State.CONNECTED){ long connectedTime = System.currentTimeMillis(); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(connectedTime); yourTextView.setText(cal.get(Calendar.HOUR_OF_DAY) + " hours, "+ cal.get(Calendar.MINUTE) + " minutes, "+ cal.get(Calendar.SECOND) + " seconds "); } else if(state == NetworkInfo.State.DISCONNECTED ){ long disconnectedTime = System.currentTimeMillis(); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(disconnectedTime); yourTextView.setText(cal.get(Calendar.HOUR_OF_DAY) + " hours, "+ cal.get(Calendar.MINUTE) + " minutes, "+ cal.get(Calendar.SECOND) + " seconds "); }