Android主进程判断

当在项目里引入多进程时,会出现 Application 类多次执行,那么此时如何进行主进程判断呢;
直接上代码(仅供参考):isMainProcess(Context context)

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.webkit.WebView;
import java.util.List;

public class ProcessUtil {

  // 包名判断是否为主进程
  public static boolean isMainProcess(Context context) {
    if (context == null) {
      return false;
    }
    return context.getPackageName().equals(getCurrentProcessName(context));
  }

  // 获取当前进程名
  public static String getCurrentProcessName(Context context) {
    if (context == null) return "";

    int pid = android.os.Process.myPid();

    String processName = "";
    ActivityManager manager = (ActivityManager) context.getSystemService(
      Context.ACTIVITY_SERVICE
    );
    for (ActivityManager.RunningAppProcessInfo process : manager.getRunningAppProcesses()) {
      if (process.pid == pid) {
        processName = process.processName;
      }
    }

    return processName;
  }

  // 异常出现情景:因为Android P行为变更,不可多进程使用同一个目录webView,需要为不同进程webView设置不同目录
  // 解决方式:重写项目Application,然后为其它进程webView设置目录
  public static void setWebDataDirectorySuffix(Application application) {
    if (application == null) {
      return;
    }

    try {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        String processName = getProcessName(application);
        if (!application.getPackageName().equals(processName)) { // 判断不等于默认进程名称
          //  为webView设置目录后缀
          WebView.setDataDirectorySuffix(processName);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private static String getProcessName(Context context) {
    if (context == null) {
      return null;
    }
    ActivityManager manager = (ActivityManager) context.getSystemService(
      Context.ACTIVITY_SERVICE
    );
    List processInfoList = manager.getRunningAppProcesses();
    if (processInfoList != null && !processInfoList.isEmpty()) {
      for (ActivityManager.RunningAppProcessInfo processInfo : processInfoList) {
        if (processInfo.pid == android.os.Process.myPid()) {
          return processInfo.processName;
        }
      }
    }
    return null;
  }
}

你可能感兴趣的:(Android主进程判断)