I was trying to get list of all application in android and calculating bytes send and recieved but the code below gives pickage names not application names:
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses();
if (runningProcesses != null && runningProcesses.size() > 0) {
// Set data to the list adapter
setListAdapter(new ListAdapter(this, runningProcesses));
}
else {
// In case there are no processes running
Toast.makeText(getApplicationContext(), "No application is running", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
long send = 0;
long recived = 0;
long wr,ws,mr,ms =0;
// Get UID of the selected process/ application
int uid = ((RunningAppProcessInfo)getListAdapter().getItem(position)).uid;
// Get traffic data
recived = TrafficStats.getUidRxBytes(uid);
send = TrafficStats.getUidTxBytes(uid);
// Get Mobile data:
mr = TrafficStats.getTotalRxBytes();
ms = TrafficStats.getMobileTxBytes();
//GetWifiDataA:
wr = (TrafficStats.getTotalRxBytes())- (TrafficStats.getMobileRxBytes());
ws = (TrafficStats.getTotalTxBytes())- (TrafficStats.getMobileTxBytes());
// Display data
Toast.makeText(getApplicationContext(), " \nUID " + uid + " details.. \nWifi Send: " +ws /1000+" \n Wifi Received: " +wr/1000+"kB"+ "\n Mobile Send: "+ms/1000+" kB"+"\n Mobile Received: "+mr/1000+"kB",Toast.LENGTH_LONG).show();
}
solved
the code with package manager solved this issue:
packageManager = getPackageManager();
List<PackageInfo> packageList = packageManager
.getInstalledPackages(PackageManager.GET_META_DATA);
apkList = (ListView) findViewById(R.id.applist);
apkList.setAdapter(new ApkAdapter(this, packageList, packageManager));
on next activity:
int app_uid = packageInfo.applicationInfo.uid;
long send = 0;
long recived = 0;
// Get traffic data
recived = TrafficStats.getUidRxBytes(app_uid);
send = TrafficStats.getUidTxBytes(app_uid);
// APP name
appLabel.setText(getPackageManager().getApplicationLabel(
packageInfo.applicationInfo));
// package name
packageName.setText(packageInfo.packageName);
// version name
version.setText(packageInfo.versionName);
// received
andVersion.setText(Long
.toString(recived));
// send
path.setText(Long.toString(send));