Created
May 31, 2016 22:53
-
-
Save rms1000watt/26569f9084a8384004b8932831c6b11e to your computer and use it in GitHub Desktop.
Get network traffic information from running services on Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.net.TrafficStats; | |
import android.app.ActivityManager; | |
import android.content.pm.PackageManager; | |
private void getServiceTraffic() { | |
PackageManager pm = getPackageManager(); | |
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); | |
List<ActivityManager.RunningServiceInfo> runningServices = am.getRunningServices(Integer.MAX_VALUE); | |
for (ActivityManager.RunningServiceInfo service : runningServices) { | |
String appName; | |
try { | |
appName = pm.getApplicationInfo(service.process, 0).loadLabel(pm).toString(); | |
} catch (PackageManager.NameNotFoundException e) { | |
appName = null; | |
} | |
int uid = service.uid; | |
long ulBytes = TrafficStats.getUidTxBytes(uid); | |
long dlBytes = TrafficStats.getUidRxBytes(uid); | |
Log.i("NET", Integer.toString(uid) +','+ appName +','+ Long.toString(ulBytes) +','+ Long.toString(dlBytes)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment