日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

android获取所有的子进程,Android M:如何获取所有进程UID?

發布時間:2025/5/22 69 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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);

}

文檔說明這不適用于生產.我也沒有做太多測試.如果不符合您的要求,請發表評論.我能想到的另一件事就是在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?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。