TrafficStats类提供总数据,因为启动后我可以直接访问来自android的
的互联网数据例如,可以直接从呼叫记录中提取呼叫历史记录。 因此可以从互联网数据使用中获取互联网数据。
如何在应用程序的基础上使用互联网数据?
答案 0 :(得分:4)
所以因为我知道我们无法直接从android获取互联网数据,因为我们从调用历史记录中获取调用日志,我们可以从TrafficStats类获取数据,这是自启动以来
要在应用程序基础上获取互联网数据,这里是使用packageManager获取相应应用程序包名称的简单代码,使用ApplicationInfo我们可以找到应用程序信息的详细信息
final PackageManager pm = context.getPackageManager();
// get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(0);
for (ApplicationInfo packageInfo : packages) {
// get the UID for the selected app
UID = packageInfo.uid;
String package_name = packageInfo.packageName;
Log.d("mypackagename",package_name+"");
ApplicationInfo app = null;
try {
app = pm.getApplicationInfo(package_name, 0);
} catch (PackageManager.NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String name = (String) pm.getApplicationLabel(app);
Drawable icon = pm.getApplicationIcon(app);
// internet usage for particular app(sent and received)
double received = (double) TrafficStats.getUidRxBytes(UID)
/ (1024 * 1024);
double send = (double) TrafficStats.getUidTxBytes(UID)
/ (1024 * 1024);
double totalab = received + send;}