process - Android M: How to get all processes UID's? -
i have application uses traffic stats api see running processes using network.
i used getting uid obtained through getrunningappprocesses()
method. apparently has been changed in android m return application package name shown here.
my question is: there way name and uid of every running processes in android m?
here sample of how doing before, recreate functionality on android m.
list<runningappprocessinfo> procinfos = activitymanager.getrunningappprocesses(); packagemanager pm = context.getpackagemanager(); (int = 0; < procinfos.size(); i++) { try { string packagename = procinfos.get(i).processname; string appname = ""; try { appname = pm.getapplicationlabel( pm.getapplicationinfo(packagename, packagemanager.get_meta_data)) .tostring(); } catch (namenotfoundexception e) { appname = ""; } int uid = procinfos.get(i).uid; long ulbytes = trafficstats.getuidtxbytes(uid); long dlbytes = trafficstats.getuidrxbytes(uid); // other stuff.
any appreciated. thanks!
you can use activitymanager.getrunningservices(int maxnum):
packagemanager pm = context.getpackagemanager(); activitymanager = (activitymanager) getsystemservice(context.activity_service); list<activitymanager.runningserviceinfo> runningservices = am.getrunningservices(integer.max_value); (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); }
the documentation states not intended production. haven't done testing either. if doesn't meet requirements leave comment. other thing can think of parsing output of running ps
in shell.
update
parsing output of ps
in shell can current running apps. example:
packagemanager pm = context.getpackagemanager(); // output of running "ps" in shell. // uses libsuperuser: https://github.com/chainfire/libsuperuser // add project: compile 'eu.chainfire:libsuperuser:1.0.0.+' list<string> stdout = shell.sh.run("ps"); list<string> packages = new arraylist<>(); (string line : stdout) { // process-name. last column. string[] arr = line.split("\\s+"); string processname = arr[arr.length - 1].split(":")[0]; packages.add(processname); } // list of installed apps on device. list<applicationinfo> apps = pm.getinstalledapplications(0); // remove apps not running. (iterator<applicationinfo> = apps.iterator(); it.hasnext(); ) { if (!packages.contains(it.next().packagename)) { it.remove(); } } (applicationinfo app : apps) { string appname = app.loadlabel(pm).tostring(); int uid = app.uid; long ulbytes = trafficstats.getuidtxbytes(uid); long dlbytes = trafficstats.getuidrxbytes(uid); /* stuff */ }
Comments
Post a Comment