yuv*_*uvi 41 android usage-statistics
我试图在每个应用程序的基础上找出Android上的数据使用情况.像Android数据使用应用程序和配额/上限监视器小部件:永远不会收取额外的数据或再次上限!.
我查看了Stack Overflow问题如何在Android环境中检测数据使用情况.
但它并没有多大帮助.
ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo( mInfo );
List<RunningAppProcessInfo> listOfRunningProcess = activityManager.getRunningAppProcesses();
Log.d(TAG, "XXSize: " + listOfRunningProcess.size());
for (RunningAppProcessInfo runningAppProcessInfo : listOfRunningProcess) {
if (runningAppProcessInfo.uid > 1026)
{
Log.d(TAG, "ANS " + runningAppProcessInfo.processName +
" Id :" + runningAppProcessInfo.pid +
" UID: " + runningAppProcessInfo.uid);
}
}
Run Code Online (Sandbox Code Playgroud)
我按照Akos Cz的建议尝试了上面的代码.但是,与上面提到的不同,所有UID都是数字app_79.这样可以吗?
Ako*_* Cz 40
以下链接可帮助您了解如何以编程方式确定每个应用程序的数据使用情况.
您需要实现代码以使用TraficStats API并跟踪每个UID(应用程序)发送/接收的字节数.
在创建新类PackageInformationTotal之后使用此方法.
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();
}
Run Code Online (Sandbox Code Playgroud)
之后,您可以以MB为单位跟踪所有流程使用情况.
| 归档时间: |
|
| 查看次数: |
67963 次 |
| 最近记录: |