我到处搜索,只找到一个对我有帮助的网站:
android.app.usage.UsageStatsManager 的 Java 代码示例
要获取有关 UsageStatsManager 的更多信息,请阅读:
UsageStatsManager
以下是获取 API 22 及以上版本中正在运行的应用程序包名称的方法:
private String getProcessName() {
String foregroundProcess = "";
ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
// Process running
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService(USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
// We get usage stats for the last 10 seconds
List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*10, time);
// Sort the stats by the last time used
if(stats != null) {
SortedMap<Long,UsageStats> mySortedMap = new TreeMap<Long,UsageStats>();
for (UsageStats usageStats : stats) {
mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);
Log.d("RunningAppProcessInfo","Package name : "+usageStats.getPackageName());
}
}
}
}
在日志中,您将能够看到正在运行的应用程序的包名称...希望对您有所帮助:)