android 获取系统名,android 获取当前进程的名称

public static String getCurrentProcessNameByActivityManager(@NonNull Context context) {

int pid = Process.myPid();

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

if (am != null) {

List runningAppList = am.getRunningAppProcesses();

if (runningAppList != null) {

for (ActivityManager.RunningAppProcessInfo processInfo : runningAppList) {

if (processInfo.pid == pid) {

return processInfo.processName;

}

}

}

}

return null;

}

缺点:

1 am.getRunningAppProcesses()需要跨进程通信,效率不高

2 ActivityManager.getRunningAppProcesses() 有可能调用失败,虽然概率不高但是当用户数量达到一定级别还是有概率失败的。

2 Application.getProcessName()

只能Api28 以上的系统调用

最优解:

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();

if (!TextUtils.isEmpty(currentProcessName)) {

return currentProcessName;

}

//3)通过ActivityManager获取当前进程名

currentProcessName = getCurrentProcessNameByActivityManager(context);

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;

}

/**

* 通过ActivityManager 获取进程名,需要IPC通信

*/

public static String getCurrentProcessNameByActivityManager(@NonNull Context context) {

if (context == null) {

return null;

}

int pid = Process.myPid();

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

if (am != null) {

List runningAppList = am.getRunningAppProcesses();

if (runningAppList != null) {

for (ActivityManager.RunningAppProcessInfo processInfo : runningAppList) {

if (processInfo.pid == pid) {

return processInfo.processName;

}

}

}

}

return null;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值