我曾经通过获取通过getRunningAppProcesses()方法获得的uID来做到这一点.显然在AndroID M中已经更改为仅返回您的应用程序包名称,如here所示.
我的问题是:还有另一种方法来获取AndroID M中每个正在运行的进程的name和UID吗?
以下是我之前如何做到这一点的示例,我想在AndroID M上重新创建此功能.
List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();PackageManager pm = context.getPackageManager();for (int i = 0; i < 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); // Do other stuff.
非常感谢任何帮助.谢谢!
解决方法 你可以使用 ActivityManager.getRunningServices(int maxNum):PackageManager pm = context.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);}
文档说明这不适用于生产.我也没有做太多测试.如果不符合您的要求,请发表评论.我能想到的另一件事就是在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<String> stdout = Shell.SH.run("ps");List<String> 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<ApplicationInfo> apps = pm.getInstalledApplications(0);// Remove apps which are not running.for (Iterator<ApplicationInfo> 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 M:如何获取所有进程UID?全部内容,希望文章能够帮你解决Android M:如何获取所有进程UID?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)