目前试了三种方法,由于21版本之前的sdk好多接口废弃了,在android7.2上getRunningTasks的方法和getRunningAppProcesses的方法都不太起作用。试到最后一种才生效。
private String focusedPackage(){
Log.d(TAG, "focusedPackage");
ActivityManager am = (ActivityManager) mContext.getSystemService(mContext.ACTIVITY_SERVICE);
List runningTaskInfos = am.getRunningTasks(6);
if(runningTaskInfos==null||runningTaskInfos.size()<=0){
Log.e(TAG,"focusedPackage falied for unknown package runningTaskInfos==null");
return null;
}
ComponentName cn = runningTaskInfos.get(0).topActivity;
if(cn==null){
Log.e(TAG,"focusedPackage falied for unknown package cn==null");
return null;
}
Log.e(TAG,"focusedPackage getPackageName here" + cn.getPackageName());
return cn.getPackageName();<