Android:获取当前打开的应用程序的堆栈(数组)

有没有办法获取当前运行或打开的Android应用程序的列表,以便我可以找到在设备中运行的最后一个应用程序。 谢谢

从几天开始我就开始使用表情符号键盘。 我需要三天才能获得前景应用程序包名称以发送表情符号图像,因为每当我想发送图像时,它只是弹出intentchooser来选择一个可以处理图像作为附加function的应用程序。

所有教程和链接都不适用于我,因为谷歌不赞成用于获取当前正在运行的应用程序的getRunningTasks()方法。

然后我有一个绝妙的想法,使用ResolveInfo在设备上获取当前正在运行的应用程序的堆栈,但它无法正常工作。

最后,我在API 21中引入了一个名为UsageStatsManager的新类, UsageStatsManagerUsageStatsManager 。 此github链接提供了如何使用此类来获取正在运行的应用程序包名称。

下面是我的代码如何在顶部运行包名称应用程序:

 public class UStats { public static final String TAG = UStats.class.getSimpleName(); @SuppressLint("SimpleDateFormat") private static final SimpleDateFormat dateFormat = new SimpleDateFormat("Md-yyyy HH:mm:ss"); public static ArrayList printCurrentUsageStatus(Context context) { return printUsageStats(getUsageStatsList(context)); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static ArrayList printUsageStats(List usageStatsList) { HashMap lastApp = new HashMap(); for (UsageStats u : usageStatsList) { lastApp.put(u.getPackageName(), (int) u.getLastTimeStamp()); /*Log.d(TAG, "Pkg: " + u.getPackageName() + "\t" + "ForegroundTime: " + u.getTotalTimeInForeground() + "\t" + "LastTimeStamp: " + new Date(u.getLastTimeStamp()));*/ } Map sortedMapAsc = sortByComparator(lastApp); ArrayList firstApp = new ArrayList<>(); for (Map.Entry entry : sortedMapAsc.entrySet()) { String key = entry.getKey(); //Integer value = entry.getValue(); firstApp.add(key); /*System.out.println("package name: " + key + ", time " + new Date(Math.abs(value)));*/ } return firstApp; } // To check the USAGE_STATS_SERVICE permission @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static List getUsageStatsList(Context context) { UsageStatsManager usm = getUsageStatsManager(context); Calendar calendar = Calendar.getInstance(); long endTime = calendar.getTimeInMillis(); calendar.add(Calendar.MINUTE, -1); long startTime = calendar.getTimeInMillis(); Log.d(TAG, "Under getUsageStateList"); Log.d(TAG, "Range start:" + dateFormat.format(startTime)); Log.d(TAG, "Range end:" + dateFormat.format(endTime)); List usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, startTime, endTime); return usageStatsList; } // Sort the map in the ascending order of the timeStamp private static Map sortByComparator(Map unsortMap) { List> list = new LinkedList<>(unsortMap.entrySet()); // Sorting the list based on values Collections.sort(list, new Comparator>() { public int compare(Map.Entry o1, Map.Entry o2) { return o2.getValue().compareTo(o1.getValue()); } }); // Maintaining insertion order with the help of LinkedList Map sortedMap = new LinkedHashMap(); for (Map.Entry entry : list) { sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; } @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1) @SuppressWarnings("ResourceType") private static UsageStatsManager getUsageStatsManager(Context context) { UsageStatsManager usm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE); return usm; } } 

然后我只需使用以下代码获取应用程序包名称的ArrayList:

 ArrayList sortedApplication = UStats.printCurrentUsageStatus(SimpleIME.this); Log.d("TAG", "applicationList: " + sortedApplication.toString()); 

别忘了添加权限:

  

以下代码检查我们的应用程序是否有权获取其他应用程序状态:

 if (UStats.getUsageStatsList(this).isEmpty()) { Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); Toast.makeText(MainActivity.this, "Enable Usage Access for YOUR_APP_NAME to use this app", Toast.LENGTH_LONG).show(); startActivity(intent); } 

上面的代码将打开一个访问设置页面,使我们的应用程序能够获取其他应用程序使用情况统计信息(一次)。

希望这会有所帮助。