获取当前前台进程的方法有很多,不过有的方法在高版本安卓可能不好使,通常5.0之前的设备可以用ActivityManager的getRunningTasks方法,5.0使用ActivityManager的getRunningAppprocesses方法,5.1及之后版本可以用UsageStatsManager的queryEvents方法
封装成一个方法如下:
public static String getTopAppPackageName(Context context) {
String packageName = "";
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
try {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
List<ActivityManager.RunningTaskInfo> rti = activityManager.getRunningTasks(1);
packageName = rti.get(0).topActivity.getPackageName();
} else if(Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
List<ActivityManager.RunningAppProcessInfo> processes = activityManager.getRunningAppProcesses();
if (processes.size() == 0) {
return packageName;
}
for (ActivityManager.RunningAppProcessInfo process : processes) {
if (process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return process.processName;
}
}
} else if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
final long end = System.currentTimeMillis();
final UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService( Context.USAGE_STATS_SERVICE);
if (null == usageStatsManager) {
return packageName;
}
final UsageEvents events = usageStatsManager.queryEvents((end - 60 * 1000), end);
if (null == events) {
return packageName;
}
UsageEvents.Event usageEvent = new UsageEvents.Event();
UsageEvents.Event lastMoveToFGEvent = null;
while (events.hasNextEvent()) {
events.getNextEvent(usageEvent);
if (usageEvent.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
lastMoveToFGEvent = usageEvent;
}
}
if (lastMoveToFGEvent != null) {
packageName = lastMoveToFGEvent.getPackageName();
}
}
}catch (Exception ignored){
}
return packageName;
}其中5.1以上的api需要申请权限:
android.permission.PACKAGE_USAGE_STATS
private boolean isNoSwitch() {
if (Build.VERSION.SDK_INT < 21) {
return true;
}
List<UsageStats> queryUsageStats = ((UsageStatsManager) getApplicationContext().
getSystemService(Context.USAGE_STATS_SERVICE))
.queryUsageStats(4, 0, System.currentTimeMillis());
return queryUsageStats != null && !queryUsageStats.isEmpty();
}if (!isNoSwitch()) {
Intent intent=new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity(intent);
}第二个方法是可以用dumpsym meminfo命令查看前台的app,匹配出来:
public static String do_exec_no_su(String cmd) {
String s = "/n";
try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
s += line + "/n";
}
} catch (IOException e) {
e.printStackTrace();
}
return s;
}public static String getForgroundPack(){
String pack="";
try {
String meminfo= do_exec_no_su(" dumpsys meminfo ");
boolean foreground = meminfo.contains("Foreground");
String[] foregrounds = meminfo.split("Foreground");
String foreground1 = foregrounds[1];
int i = foreground1.indexOf("(pid");
foreground1= foreground1.substring(0,i).trim();
int i1 = foreground1.indexOf("kB:");
pack = foreground1.substring(i1 + 3).trim().replace(" ","");
} catch (Exception e) {
LogUtil.e("出错:"+e.getMessage());
e.printStackTrace();
}
return pack;
}第三个方法是直接读取/proc内容,遍历里面的文件,oom_score_adj值为0的
private static String read(String path) throws IOException {
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(path));
output.append(reader.readLine());
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
output.append('\n').append(line);
}
reader.close();
return output.toString();
} String () {
File[] files = File().listFiles()String foregroundProcess = i = (File file : files) {
i++Log.(+ file.getName()
+ + i)(file.isFile()) {
}
pidLog.(+ file.getName()){
pid = Integer.(file.getName())} (NumberFormatException e) {
}
{
String cmdline = (String.(pid))String oomAdj = (String.(pid))Log.(+ oomAdj + + cmdline)(oomAdj.equalsIgnoreCase()) {
Log.(+ oomAdj + + cmdline)} {
}
(cmdline.contains() || cmdline.contains()) {
}
foregroundProcess = cmdline} (IOException e) {
e.printStackTrace()}
}
foregroundProcess}本文为Adamin90原创文章,转载无需和我联系,但请注明来自http://www.lixiaopeng.top
