android获取所有的子进程,Android M:如何获取所有进程UID?
你可以使用
ActivityManager.getRunningServices(int maxNum):
PackageManager pm = context.getPackageManager();
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List 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);
}
文檔說明這不適用于生產(chǎn).我也沒有做太多測試.如果不符合您的要求,請發(fā)表評論.我能想到的另一件事就是在shell中解析運行ps的輸出.
UPDATE
在shell中解析ps的輸出,我們可以獲得當前正在運行的應用程序.例:
PackageManager pm = context.getPackageManager();
// Get the output of running "ps" in a shell.
// This uses libsuperuser: https://github.com/Chainfire/libsuperuser
// To add this to your project: compile 'eu.chainfire:libsuperuser:1.0.0.+'
List stdout = Shell.SH.run("ps");
List packages = new ArrayList<>();
for (String line : stdout) {
// Get the process-name. It is the last column.
String[] arr = line.split("\\s+");
String processName = arr[arr.length - 1].split(":")[0];
packages.add(processName);
}
// Get a list of all installed apps on the device.
List apps = pm.getInstalledApplications(0);
// Remove apps which are not running.
for (Iterator it = apps.iterator(); it.hasNext(); ) {
if (!packages.contains(it.next().packageName)) {
it.remove();
}
}
for (ApplicationInfo app : apps) {
String appName = app.loadLabel(pm).toString();
int uid = app.uid;
long ulBytes = TrafficStats.getUidTxBytes(uid);
long dlBytes = TrafficStats.getUidRxBytes(uid);
/* do your stuff */
}
總結
以上是生活随笔為你收集整理的android获取所有的子进程,Android M:如何获取所有进程UID?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android sdk投屏,海豚星空扫码
- 下一篇: 根据图片原型写一个html页面,原型图的