public static boolean isMainProgress(Context context){
boolean isMain = false;
try {
String prosessName = getCurrentProcessName(context);
String pacakgeName = context.getApplicationContext().getPackageName();
if(!TextUtils.isEmpty(prosessName)
&& !TextUtils.isEmpty(pacakgeName)
&& prosessName.equals(pacakgeName)){
isMain = true;
}
} catch (Exception e){
}
return isMain;
}
/**
* 获取子进程名称,使用包名分割
*/
public static String getSubProcessName(Context context) {
String subProcessName = "";
try {
String processName = getCurrentProcessName(context);
int index = processName.indexOf(":");
if (index >= 0) {
subProcessName = processName.substring(index + 1);
}
if (index < 0) {
subProcessName = processName;
}
} catch (Throwable e) {
}
return subProcessName;
}
/**
* 获取当前进程名
*/
public static String getCurrentProcessName(Context context) {
if (TextUtils.isEmpty(sCurrentProcessName)) {
try {
String processName = "";
int pid = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
processName = Application.getProcessName();
} else {
pid = android.os.Process.myPid();
processName = getProcessName(pid);
}
if (!TextUtils.isEmpty(processName)) {
sCurrentProcessName = processName;
}
} catch (Throwable e) {
}
}
return sCurrentProcessName;
}
private static String getProcessName(int pid) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
String processName = reader.readLine();
if (!TextUtils.isEmpty(processName)) {
processName = processName.trim();
}
return processName;
} catch (Throwable t) {
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (Throwable e) {
}
}
return null;
}```