-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathProcessUtils.java
More file actions
39 lines (32 loc) · 1.17 KB
/
ProcessUtils.java
File metadata and controls
39 lines (32 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.realmo.utils;
import android.app.ActivityManager;
import android.content.Context;
import android.text.TextUtils;
import java.util.List;
public class ProcessUtils {
/**
* 获取顶端APK 包名
* @param context
* @return
*/
public static String getCurrentAppPackage(Context context) {
String result = "";
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (android.os.Build.VERSION.SDK_INT < 21) {
// 如果没有就用老版本
List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
if (runningTaskInfos != null && runningTaskInfos.size() > 0) {
result = runningTaskInfos.get(0).topActivity.getPackageName();
}
} else {
List<ActivityManager.RunningAppProcessInfo> runningApp = manager.getRunningAppProcesses();
if (runningApp != null && runningApp.size() > 0) {
result = runningApp.get(0).processName;
}
}
if (TextUtils.isEmpty(result)) {
result = "";
}
return result;
}
}