From eef4cb18a0af09730d93c4755abf2dab025c6b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B5=B7=E4=BC=A6?= <1217003634@qq.com> Date: Fri, 16 Sep 2022 10:47:46 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9A=90=E7=A7=81=E5=90=88=E8=A7=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/kwai/koom/base/Monitor_Process.kt | 16 +---- .../com/kwai/koom/base/utils/ProcessUtil.java | 68 +++++++++++++++++++ 2 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 koom-monitor-base/src/main/java/com/kwai/koom/base/utils/ProcessUtil.java diff --git a/koom-monitor-base/src/main/java/com/kwai/koom/base/Monitor_Process.kt b/koom-monitor-base/src/main/java/com/kwai/koom/base/Monitor_Process.kt index f9b5da7f..033fcc00 100644 --- a/koom-monitor-base/src/main/java/com/kwai/koom/base/Monitor_Process.kt +++ b/koom-monitor-base/src/main/java/com/kwai/koom/base/Monitor_Process.kt @@ -23,6 +23,7 @@ import android.content.Context import android.os.Build import android.os.Process import com.kwai.koom.base.MonitorManager.getApplication +import com.kwai.koom.base.utils.ProcessUtil import java.io.File enum class Abi { @@ -92,18 +93,5 @@ private fun getProcessNameByProc(): String? { } private fun getProcessNameByAms(): String? { - try { - val activityManager = MonitorManager.getApplication().getSystemService(Context.ACTIVITY_SERVICE) - as ActivityManager - - val appProcessList = activityManager.runningAppProcesses - for (processInfo in appProcessList.orEmpty()) { - if (processInfo.pid == Process.myPid()) { - return processInfo.processName - } - } - } catch (e: Exception) { - e.printStackTrace() - } - return null + return ProcessUtil.getCurrentProcessName(MonitorManager.getApplication()) } \ No newline at end of file diff --git a/koom-monitor-base/src/main/java/com/kwai/koom/base/utils/ProcessUtil.java b/koom-monitor-base/src/main/java/com/kwai/koom/base/utils/ProcessUtil.java new file mode 100644 index 00000000..0f4cd9eb --- /dev/null +++ b/koom-monitor-base/src/main/java/com/kwai/koom/base/utils/ProcessUtil.java @@ -0,0 +1,68 @@ +package com.kwai.koom.base.utils; + +import android.app.Application; +import android.content.Context; +import android.os.Build; +import android.text.TextUtils; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.lang.reflect.Method; + +/** + * @Created by Hailun on 2022/8/23 15:52 + * @Description 获取当前进程名工具类 + */ +public class ProcessUtil { + private static String currentProcessName; + /** + * @return 当前进程名 + */ + @Nullable + public static String getCurrentProcessName(@NonNull Context context) { + if (!TextUtils.isEmpty(currentProcessName)) { + return currentProcessName; + } + + //1)通过Application的API获取当前进程名 + currentProcessName = getCurrentProcessNameByApplication(); + if (!TextUtils.isEmpty(currentProcessName)) { + return currentProcessName; + } + + //2)通过反射ActivityThread获取当前进程名 + currentProcessName = getCurrentProcessNameByActivityThread(); + + return currentProcessName; + } + + /** + * 通过Application新的API获取进程名,无需反射,无需IPC,效率最高。 + */ + public static String getCurrentProcessNameByApplication() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + return Application.getProcessName(); + } + return null; + } + + /** + * 通过反射ActivityThread获取进程名,避免了ipc + */ + public static String getCurrentProcessNameByActivityThread() { + String processName = null; + try { + final Method declaredMethod = Class.forName("android.app.ActivityThread", false, Application.class.getClassLoader()) + .getDeclaredMethod("currentProcessName", (Class[]) new Class[0]); + declaredMethod.setAccessible(true); + final Object invoke = declaredMethod.invoke(null, new Object[0]); + if (invoke instanceof String) { + processName = (String) invoke; + } + } catch (Throwable e) { + e.printStackTrace(); + } + return processName; + } +}