我想查找Android中互联网使用的开始时间和结束时间?我怎么找到它?我需要使用哪些软件包?Android上的数据使用情况
2
A
回答
0
您可以注册WiFi和移动网络BroadcastReceiver
,然后保存时间在BroadcastReceiver
类
public class NetworkReceiver extends BroadcastReceiver {
private final String TAG = "WifiReceiver";
public static final String WIFI_DISCONNECTED_TIME = "wifi_disconnected_time";
public static final String WIFI_CONNECTED_TIME = "wifi_connected_time";
@Override
public void onReceive(Context context, Intent intent) {
MyLog.e(TAG, "Broadcast");
if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
String networkStateText;
switch (netInfo.getState()) {
case DISCONNECTING:
networkStateText = "DISCONNECTING";
break;
case DISCONNECTED:
networkStateText = "DISCONNECTED";
//Save the time internet disconnected here
context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE).edit().putLong(WIFI_DISCONNECTED_TIME, System.currentTimeMillis()).commit();
break;
case CONNECTING:
networkStateText = "CONNECTING";
break;
case CONNECTED:
networkStateText = "CONNECTED";
//Save the time internet connected here
context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE).edit().putLong(WIFI_CONNECTED_TIME, System.currentTimeMillis()).commit();
break;
case SUSPENDED:
networkStateText = "SUSPENDED";
break;
case UNKNOWN:
networkStateText = "UNKNOWN";
break;
default:
networkStateText = "No State";
break;
}
MyLog.e(TAG, "Broadcast > NetworkState: " + networkStateText);
}
}
}
不要忘了把许可android.permission.ACCESS_NETWORK_STATE
并添加接收器Android清单的onReceive()
方法。
<!-- Check for Internet Connectivity -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name=".NetworkReceiver" >
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
</application>
然后你就可以从你的SharedPreferences
的时间在你Activity
类。
祝你好运。
2
萨兰,
你要使用TrafficStats
类。
相关问题
- 1. 数据使用情况+应用程序数据使用情况
- 2. Android数据库使用情况
- 3. Android设备上的jGit使用情况
- 4. 在不使用数据库的情况下存储数据android
- 5. Java中的数据流使用情况
- 6. R中的数据帧使用情况
- 7. 跟踪iPhone的数据使用情况
- 8. Android内存使用情况
- 9. 如何检索iphone上的互联网数据使用情况
- 10. iPhone上的负值数据使用情况?
- 11. lucene在trec数据上的使用情况
- 12. COUNT数据使用情况下与mysql
- 13. 检测蜂窝数据使用情况
- 14. JDBC setMaxRows数据库使用情况
- 15. JS监控数据使用情况?
- 16. 星火数据框中使用情况
- 17. 单词使用情况数据库?
- 18. 移动数据使用情况
- 19. 如何在Android环境中检测数据使用情况
- 20. android 4.0 spinner的使用情况
- 21. Android加速度计的使用情况
- 22. 分析数据库中的数据类型使用情况
- 23. 如何确定SQLite数据库的数据库使用情况?
- 24. Android应用程序使用情况
- 25. Android应用内存使用情况
- 26. Android上的跟踪电源使用情况
- 27. 缓存Android上的SVG图像和内存使用情况
- 28. 在没有生根的情况下在Android上使用libusb
- 29. 如何监控Android上CPU的使用情况?
- 30. 监控Android设备上的GPS使用情况