RRT*_*RTW 15 android traffic network-traffic traffic-measurement 3g
对于每个APP的统计网络流量,我现在使用的是Android TrafficStats
我可以获得如下结果:
据我所知,"Android Trafficstats"只是一个指向ac文件的本机指针.(也许是.so?)
但它混合了Wifi和3g流量,有没有办法只获得非WiFi流量统计?
晚上,我有办法做到这一点......
首先,我必须创建一个扩展BroadcasrReceiver的类,如下所示:
清单定义:
<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>
Run Code Online (Sandbox Code Playgroud)
代码:
/**
* @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...
}
}
}
Run Code Online (Sandbox Code Playgroud)
还有一个手机统计监听器......
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
最后,这是我的逻辑
据我所知,如果3G和WiFi都可用,网络流量将仅通过WiFi.
小智 5
经过长期的努力,我能够找到解决方案,以便通过Android设备中每个已安装的应用程序通过任何接口获取数据。
由于Android提供了TrafficStats Apis,但是这些API正在为每个应用程序uid提供编译数据统计信息,因为设备启动和Even API不支持通过特定应用程序的任何接口获取数据。即使我们依赖于TraffiucStates APIS,我们也会为每个应用程序获得新的数据统计信息。
所以我想使用隐藏的API来使用它。
在这里,我提到的是通过Android中的任何接口获取每个应用程序的数据统计信息的步骤...
建立“ INetworkStatsSession”会话
import android.net.INetworkStatsSession;
INetworkStatsSession mStatsSession = mStatsService.openSession();
Run Code Online (Sandbox Code Playgroud)根据您要测量的接口创建网络模板。
import static android.net.NetworkTemplate.buildTemplateEthernet;
import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
import static android.net.NetworkTemplate.buildTemplateMobile4g;
import static android.net.NetworkTemplate.buildTemplateMobileAll;
import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
import android.net.NetworkTemplate;
private NetworkTemplate mTemplate;
mTemplate = buildTemplateMobileAll(getActiveSubscriberId(this
.getApplicationContext()));
Run Code Online (Sandbox Code Playgroud)GetActive SubscriberID:
private static String getActiveSubscriberId(Context context) {
final TelephonyManager tele = TelephonyManager.from(context);
final String actualSubscriberId = tele.getSubscriberId();
return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
}
Run Code Online (Sandbox Code Playgroud)通过传递应用程序UID来收集各个应用程序的网络历史记录...
private NetworkStatsHistory collectHistoryForUid(NetworkTemplate template,
int uid, int set) throws RemoteException {
final NetworkStatsHistory history = mStatsSession.getHistoryForUid(
template, uid, set, TAG_NONE, FIELD_RX_BYTES | FIELD_TX_BYTES);
return history;
}
Run Code Online (Sandbox Code Playgroud)获取总消费数据:
public void showConsuption(int UID){
NetworkStatsHistory history = collectHistoryForUid(mTemplate, UID,
SET_DEFAULT);
Log.i(DEBUG_TAG, "load:::::SET_DEFAULT:.getTotalBytes:"+ Formatter.formatFileSize(context, history.getTotalBytes()));
history = collectHistoryForUid(mTemplate, 10093,
SET_FOREGROUND);
Log.i(DEBUG_TAG, "load::::SET_FOREGROUND::.getTotalBytes:"+ Formatter.formatFileSize(context, history.getTotalBytes()));
history = collectHistoryForUid(mTemplate, 10093,
SET_ALL);
Log.i(DEBUG_TAG, "load::::SET_ALL::.getTotalBytes:"+ Formatter.formatFileSize(context, history.getTotalBytes()));
}
Run Code Online (Sandbox Code Playgroud) 归档时间: |
|
查看次数: |
9958 次 |
最近记录: |