简体   繁体   中英

(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.

Is there some method can get the information what i need?(example If can)

Thanks.

1 : 在此处输入图片说明

1 : 在此处输入图片说明

you can get the UsesStats of the top foreground Applications. 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();                                   
        }                                       
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM