[英](Android) detect when another app is launched / launch count of all app
I would like to get the information about another app last time used , or launch time ,or launch count of another app. 我想获取有关上次使用的另一个应用程序的信息,或启动时间,或另一个应用程序的启动次数。 (Just like the information of usage statistics)
(就像使用情况统计信息一样)
Usage statistics information 使用情况统计 信息
I have found some similar question and answer at here. 我在这里找到了类似的问题和答案。 But those doesn't work in android M or android L.
但是这些在android M或android L中不起作用。
Is there some method can get the information what i need?(example If can) 有什么方法可以获取我需要的信息吗(例如,可以)
Thanks. 谢谢。
you can get the UsesStats
of the top foreground Applications. 您可以获取最
UsesStats
的前台应用程序的UsesStats
。 but using this the user have rights to Accept or Decline for each app separately. 但使用此功能,用户有权分别为每个应用接受或拒绝 。 getting the permission
获得许可
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
and this will return the foreground applications.. 这将返回前台应用程序。
String topPackageName ;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService("usagestats");
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);
}
if(!mySortedMap.isEmpty()) {
topPackageName = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.