每个APP安卓统计3G流量,怎么样? [英] android statistic 3g traffic for each APP, how?
问题描述
有关每个APP统计网络流量,我现在使用的是的Android TrafficStats
For statistic network traffic per APP, what I'm using now is Android TrafficStats
这是我能得到类似结果如下:
That I can get result like following :
- 的Youtube 50.30兆字节
- 在Facebook的21.39兆字节
- 在谷歌播放103.38兆字节
- (更多...)
据我所知,Android的Trafficstats只是一个本地指针交流文件。 (也许是。所以?)
As I know, the "Android Trafficstats" just a native pointer to a c file. (maybe an .so ?)
不过,混合Wifi和放大器; 3G流量,有没有办法只能获得非无线网络流量统计?
But it mixed Wifi & 3g traffic, is there any way to only get non-WiFi traffic statistic ?
推荐答案
晚上好,我得到了一些方法来做到这一点......
Evening all, I got some way to do that...
首先,我必须创建一个类延伸BroadcasrReceiver,像这样的:
First I have to create a class which extends BroadcasrReceiver, like this:
清单定义:
<receiver android:name=".core.CoreReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
codeS:
Codes:
/**
* @author me
*/
public class CoreReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (Constants.phone == null) {
// Receive [network] event
Constants.phone=new PhoneListen(context);
TelephonyManager telephony=(TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(Constants.phone, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
}
WifiManager wifi=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
boolean b=wifi.isWifiEnabled();
if (Constants.STATUS_WIFI != b) {
// WiFi status changed...
}
}
}
和下方的电话统计听众......
And a phone stats listener below...
public class PhoneListen extends PhoneStateListener {
private Context context;
public PhoneListen(Context c) {
context=c;
}
@Override
public void onDataConnectionStateChanged(int state) {
switch(state) {
case TelephonyManager.DATA_DISCONNECTED:// 3G
//3G has been turned OFF
break;
case TelephonyManager.DATA_CONNECTING:// 3G
//3G is connecting
break;
case TelephonyManager.DATA_CONNECTED:// 3G
//3G has turned ON
break;
}
}
}
最后,这是我的逻辑
Finally, here's my logic
- 收集计数到的SQLite数据库。
- 收集通过TrafficStats所有应用程序的网络使用情况每隔1分钟,只有当3G为ON。
- 如果3G为OFF,然后停止收集。
- 如果两个3G和放大器;无线网络是ON时,停止收取。
据我所知,网络流量将通过无线网络而已,如果两个3G和放大器;无线网络是可用的。
As I know, network traffic will go through WiFi only, if both 3G & WiFi are available.
这篇关于每个APP安卓统计3G流量,怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!