Is there a way to get current process name in Android

后端 未结 11 1748
-上瘾入骨i
-上瘾入骨i 2020-12-03 00:59

I set an Android:process=\":XX\" for my particular activity to make it run in a separate process. However when the new activity/process init, it will call my Application:onC

相关标签:
11条回答
  • 2020-12-03 01:10

    I have more efficient method, you don't need IPC to ActivityManagerService and poll the Running process, or read the file.You can call this method from your custom Application class;

     private String getProcessName(Application app) {
        String processName = null;
        try {
            Field loadedApkField = app.getClass().getField("mLoadedApk");
            loadedApkField.setAccessible(true);
            Object loadedApk = loadedApkField.get(app);
    
            Field activityThreadField = loadedApk.getClass().getDeclaredField("mActivityThread");
            activityThreadField.setAccessible(true);
            Object activityThread = activityThreadField.get(loadedApk);
    
            Method getProcessName = activityThread.getClass().getDeclaredMethod("getProcessName", null);
            processName = (String) getProcessName.invoke(activityThread, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return processName;
    }
    

    ActivityManagerService is already send the process infor to ActivityThread when process is start.(ActivityThread.main-->attach()-->IActivityManager.attachApplication--IPC-->ActivityManagerService-->ApplicationThread.bindApplication)

    ApplicationThread:
    public final void bindApplication(String processName,***) {
        //***
        AppBindData data = new AppBindData();
        data.processName = processName;
        //**
    }
    

    When we called getProcessName, it will finally deliver to AppBindData object. So we can easily and efficient get current process name;

    0 讨论(0)
  • 2020-12-03 01:14

    Full code is

        String currentProcName = "";
        int pid = android.os.Process.myPid();
        ActivityManager manager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningAppProcessInfo processInfo : manager.getRunningAppProcesses())
        {
            if (processInfo.pid == pid)
            {
                currentProcName = processInfo.processName;
                return;
            }
        }
    
    0 讨论(0)
  • 2020-12-03 01:15

    The ActivityManager solution contains a sneaky bug, particularly if you check your own process name from your Application object. Sometimes, the list returned from getRunningAppProcesses simply doesn't contain your own process, raising a peculiar existential issue.

    The way I solve this is

        BufferedReader cmdlineReader = null;
        try {
            cmdlineReader = new BufferedReader(new InputStreamReader(
                new FileInputStream(
                    "/proc/" + android.os.Process.myPid() + "/cmdline"),
                "iso-8859-1"));
            int c;
            StringBuilder processName = new StringBuilder();
            while ((c = cmdlineReader.read()) > 0) {
                processName.append((char) c);
            }
            return processName.toString();
        } finally {
            if (cmdlineReader != null) {
                cmdlineReader.close();
            }
        }
    

    EDIT: Please notice that this solution is much faster than going through the ActivityManager but does not work if the user is running Xposed or similar. In that case you might want to do the ActivityManager solution as a fallback strategy.

    0 讨论(0)
  • 2020-12-03 01:16

    There is a method in ActivityThread class, You may use reflection to get the current processName. You don't need any loop or tricks. The performance is best compares to other solution. The limitation is you can only get your own process name. It's not a big deal since it covers most usage cases.

        val activityThreadClass = XposedHelpers.findClass("android.app.ActivityThread", param.classLoader)
        val activityThread = XposedHelpers.callStaticMethod(activityThreadClass, "currentActivityThread")
        val processName = XposedHelpers.callStaticMethod(activityThreadClass, "currentProcessName")
    
    0 讨论(0)
  • 2020-12-03 01:19

    If I've understood your question correctly, you should be able to use ActivityManager, as per this thread.

    0 讨论(0)
  • 2020-12-03 01:25

    This is an update to David Burström's answer. This can be written far more concisely as:

    public String get() {
      final File cmdline = new File("/proc/" + android.os.Process.myPid() + "/cmdline");
      try (BufferedReader reader = new BufferedReader(new FileReader(cmdline))) {
        return reader.readLine();
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
    
    0 讨论(0)
提交回复
热议问题