我正在尝试获取所有应用程序在上个月(或一周)或给定时间段之间使用的移动数据。我想获取移动数据使用历史记录。我看到android现在默认这样做了。是否可以通过代码获取此信息?
问问题
1965 次
2 回答
2
是否可以通过代码获取此信息?
至少通过 Android 4.4,没有任何记录或支持的方式。不过,欢迎您通过定期检查TrafficStats
自己收集这些数据。
于 2014-08-10T16:24:31.100 回答
0
试试这个代码
public void getPakagesInfoUsingHashMap() {
final PackageManager pm = getPackageManager();
// get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(0);
// loop through the list of installed packages and see if the selected
// app is in the list
for (ApplicationInfo packageInfo : packages) {
// get the UID for the selected app
UID = packageInfo.uid;
String package_name = packageInfo.packageName;
ApplicationInfo app = null;
try {
app = pm.getApplicationInfo(package_name, 0);
} catch (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 total = received + send;
if(total>0)
{
PackageInformationTotal pi=new PackageInformationTotal();
pi.name=name;
pi.packageName=package_name;
pi.icon=icon;
pi.totalMB=String.format( "%.2f", total )+" MB";
pi.individual_mb=String.format( "%.2f", total );
totalData+=Double.parseDouble(String.format( "%.2f", total ));
dataHash.add(pi);
Log.e(name,String.format( "%.2f", total )+" MB");
}
}
Editor edit=shared.edit();
edit.putString("Total",String.format( "%.2f", totalData));
edit.commit();
}
以下链接应该可以帮助您了解如何以编程方式确定每个应用程序的数据使用情况。
https://github.com/commonsguy/cw-andtuning/tree/master/TrafficMonitor
http://agolovatyuk.blogspot.com/2012/04/android-traffic-statistics-inside.html
您将需要实现您的代码以使用 TraficStats API 并跟踪每个 UID(应用程序)发送/接收的字节数。
于 2014-08-29T19:47:27.373 回答