我试图获得所有应用程序在前一个月(或一周)或给定时间段之间使用的移动数据。我想要手机数据的使用记录。我看到android现在默认是这样做的。有可能通过代码获得这些信息吗?
有可能通过代码获得这些信息吗?
没有任何文档或支持的方式,至少通过Android 4.4。但是,欢迎您通过定期检查TrafficStats
来收集这些数据。
试试这个代码
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://www.techrepublic.com/blog/app-builder/create-a-network-monitor-using-androids-trafficstats-class/774 http://agolovatyuk.blogspot.com/2012/04/android-traffic-statistics-inside.html您将需要实现您的代码来使用trafficstats API并跟踪每个UID(应用程序)发送/接收的字节数。