當前位置: 首頁>>代碼示例>>Java>>正文


Java UsageStatsManager類代碼示例

本文整理匯總了Java中android.app.usage.UsageStatsManager的典型用法代碼示例。如果您正苦於以下問題:Java UsageStatsManager類的具體用法?Java UsageStatsManager怎麽用?Java UsageStatsManager使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


UsageStatsManager類屬於android.app.usage包,在下文中一共展示了UsageStatsManager類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: retrieveStats

import android.app.usage.UsageStatsManager; //導入依賴的package包/類
private void retrieveStats() {
    UsageStatsManager manager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
    List<UsageStats> stats = manager.queryUsageStats(UsageStatsManager.INTERVAL_BEST,
            BEGIN, System.currentTimeMillis());
    if (stats == null) {
        UILog.i("no stats");
        return;
    }
    for (UsageStats stat : stats) {
        String packageName = stat.getPackageName();
        int key = mStats.indexOfKey(packageName);
        if (key >= 0) {
            mStats.valueAt(key).add(stat);
        } else {
            mStats.put(packageName, new UsageStats(stat));
        }
    }
}
 
開發者ID:brevent,項目名稱:Brevent,代碼行數:19,代碼來源:BreventActivity.java

示例2: getForegroundPackage

import android.app.usage.UsageStatsManager; //導入依賴的package包/類
public static String getForegroundPackage(UsageStatsManager usageStatsManager) {
    String packageName = null;

    final long INTERVAL = 1000 * 60;
    final long end = System.currentTimeMillis();
    final long begin = end - INTERVAL;
    final UsageEvents usageEvents = usageStatsManager.queryEvents(begin, end);
    while (usageEvents.hasNextEvent()) {
        UsageEvents.Event event = new UsageEvents.Event();
        usageEvents.getNextEvent(event);
        switch (event.getEventType()) {
            case UsageEvents.Event.MOVE_TO_FOREGROUND:
                packageName = event.getPackageName();
                break;
            case UsageEvents.Event.MOVE_TO_BACKGROUND:
                if (event.getPackageName().equals(packageName)) {
                    packageName = null;
                }
        }
    }

    return packageName;
}
 
開發者ID:RikkaApps,項目名稱:FCM-for-Mojo,代碼行數:24,代碼來源:UsageStatsUtils.java

示例3: getForegroundApp

import android.app.usage.UsageStatsManager; //導入依賴的package包/類
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public String getForegroundApp(final Context context) {
    if(!Utils.hasUsageStatsPermission(context))
        return null;

    String foregroundApp = null;

    UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Service.USAGE_STATS_SERVICE);
    long time = System.currentTimeMillis();

    UsageEvents usageEvents = mUsageStatsManager.queryEvents(time - 1000 * 3600, time);
    UsageEvents.Event event = new UsageEvents.Event();
    while (usageEvents.hasNextEvent()) {
        usageEvents.getNextEvent(event);
        if(event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
            foregroundApp = event.getPackageName();
        }
    }

    return foregroundApp ;
}
 
開發者ID:ricvalerio,項目名稱:foregroundappchecker,代碼行數:22,代碼來源:LollipopDetector.java

示例4: getStats

import android.app.usage.UsageStatsManager; //導入依賴的package包/類
@SuppressWarnings("ResourceType")
public static void getStats(Context context){
    UsageStatsManager usm = (UsageStatsManager) context.getSystemService("usagestats");
    int interval = UsageStatsManager.INTERVAL_YEARLY;
    Calendar calendar = Calendar.getInstance();
    long endTime = calendar.getTimeInMillis();
    calendar.add(Calendar.YEAR, -1);
    long startTime = calendar.getTimeInMillis();

    Log.d(TAG, "Range start:" + dateFormat.format(startTime) );
    Log.d(TAG, "Range end:" + dateFormat.format(endTime));

    UsageEvents uEvents = usm.queryEvents(startTime,endTime);
    while (uEvents.hasNextEvent()){
        UsageEvents.Event e = new UsageEvents.Event();
        uEvents.getNextEvent(e);

        if (e != null){
            Log.d(TAG, "Event: " + e.getPackageName() + "\t" +  e.getTimeStamp());
        }
    }
}
 
開發者ID:maoruibin,項目名稱:AppPlus,代碼行數:23,代碼來源:UStats.java

示例5: isGoRunning

import android.app.usage.UsageStatsManager; //導入依賴的package包/類
@Override
public boolean isGoRunning() {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        UsageStatsManager usm = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
        long time = System.currentTimeMillis();
        List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - REFRESH_INTERVAL * REFRESH_INTERVAL, time);
        if (appList != null && appList.size() > 0) {
            SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>();
            for (UsageStats usageStats : appList) {
                mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
            }
            if (!mySortedMap.isEmpty()) {
                String currentAppName = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
                return (currentAppName.equals("com.android.systemui") || currentAppName.equals("com.tomer.poke.notifier") || currentAppName.equals(getPackageName())) ? isGoOpen : currentAppName.equals(Constants.GOPackageName);
            }
        }
    } else {
        ActivityManager am = (ActivityManager) getBaseContext().getSystemService(ACTIVITY_SERVICE);
        return am.getRunningTasks(1).get(0).topActivity.getPackageName().equals(Constants.GOPackageName);
    }
    return isGoOpen;
}
 
開發者ID:rosenpin,項目名稱:Enhancer-For-GO,代碼行數:23,代碼來源:MainService.java

示例6: getAppEntriesUsingUsageStats

import android.app.usage.UsageStatsManager; //導入依賴的package包/類
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
private List<AppEntry> getAppEntriesUsingUsageStats() {
    UsageStatsManager mUsageStatsManager = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
    List<UsageStats> usageStatsList = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_YEARLY, searchInterval, System.currentTimeMillis());
    List<AppEntry> entries = new ArrayList<>();

    for(UsageStats usageStats : usageStatsList) {
        AppEntry newEntry = new AppEntry(
                usageStats.getPackageName(),
                null,
                null,
                null,
                false
        );

        newEntry.setTotalTimeInForeground(usageStats.getTotalTimeInForeground());
        newEntry.setLastTimeUsed(usageStats.getLastTimeUsed());
        entries.add(newEntry);
    }

    return entries;
}
 
開發者ID:farmerbb,項目名稱:Taskbar,代碼行數:23,代碼來源:TaskbarService.java

示例7: isGoRunning

import android.app.usage.UsageStatsManager; //導入依賴的package包/類
@Override
public boolean isGoRunning() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        UsageStatsManager usm = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
        long time = System.currentTimeMillis();
        List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - REFRESH_INTERVAL * REFRESH_INTERVAL, time);
        if (appList != null && appList.size() > 0) {
            SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>();
            for (UsageStats usageStats : appList) {
                mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
            }
            if (!mySortedMap.isEmpty()) {
                String currentAppName = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
                return (currentAppName.equals("com.android.systemui") || currentAppName.equals("com.tomer.poke.notifier") || currentAppName.equals(getPackageName())) ? isGoOpen : currentAppName.equals(Constants.GOPackageName);
            }
        }
    } else {
        ActivityManager am = (ActivityManager) getBaseContext().getSystemService(ACTIVITY_SERVICE);
        return am.getRunningTasks(1).get(0).topActivity.getPackageName().equals(Constants.GOPackageName);
    }
    return isGoOpen;
}
 
開發者ID:rosenpin-deprecated,項目名稱:More-For-GO,代碼行數:23,代碼來源:MainService.java

示例8: queryUsageStats

import android.app.usage.UsageStatsManager; //導入依賴的package包/類
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static String queryUsageStats() {
    String packageName=null;
    UsageStatsManager usm = (UsageStatsManager) FingerApp.get().getSystemService(Context.USAGE_STATS_SERVICE);
    long time = System.currentTimeMillis();
    List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
            time - 1000 * 1000, time);
    if (appList != null && appList.size() > 0) {
        SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
        for (UsageStats usageStats : appList) {
            mySortedMap.put(usageStats.getLastTimeUsed(),
                    usageStats);
        }
        if (!mySortedMap.isEmpty()) {
             packageName = mySortedMap.get(
                    mySortedMap.lastKey()).getPackageName();
        }
    }
    return packageName;
}
 
開發者ID:kevinhqf,項目名稱:FingerTrans,代碼行數:21,代碼來源:AppUtils.java

示例9: getTaskTopAppPackageName

import android.app.usage.UsageStatsManager; //導入依賴的package包/類
/**
 * get current task top app package name
 * @param context
 * @param am
 * @return the top app package name
 */
public static String getTaskTopAppPackageName(Context context, ActivityManager am) {
    String packageName = "";
    // if the sdk >= 21. It can only use getRunningAppProcesses to get task top package name
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        UsageStatsManager usage = (UsageStatsManager)context.getSystemService(Context.USAGE_STATS_SERVICE);
        long time = System.currentTimeMillis();
        List<UsageStats> stats = usage.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 10, time);
        if (stats != null) {
            SortedMap<Long, UsageStats> runningTask = new TreeMap<Long,UsageStats>();
            for (UsageStats usageStats : stats) {
                runningTask.put(usageStats.getLastTimeUsed(), usageStats);
            }
            if (runningTask.isEmpty()) {
                return null;
            }
            packageName =  runningTask.get(runningTask.lastKey()).getPackageName();
        }
    } else {// if sdk <= 20, can use getRunningTasks
        List<ActivityManager.RunningTaskInfo> infos = am.getRunningTasks(1);
        packageName = infos.get(0).topActivity.getPackageName();
    }
    return packageName;
}
 
開發者ID:ittianyu,項目名稱:MobileGuard,代碼行數:30,代碼來源:ProcessManagerEngine.java

示例10: updateRecentApps

import android.app.usage.UsageStatsManager; //導入依賴的package包/類
private void updateRecentApps() {
    Blacklist blacklist = Blacklist.getInstance(this);
    String currentForegroundApp = null;

    if(!U.isNightModeOn(this))
        stopSelf();
    else if(Settings.Secure.getInt(getContentResolver(), "accessibility_display_inversion_enabled", 0) == 1)
        U.setTint(this, false);
    else {
        if(blacklist.getBlockedApps().size() > 0) {
            UsageStatsManager mUsageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
            UsageEvents events = mUsageStatsManager.queryEvents(
                    System.currentTimeMillis() - AlarmManager.INTERVAL_DAY,
                    System.currentTimeMillis());

            UsageEvents.Event eventCache = new UsageEvents.Event();

            while(events.hasNextEvent()) {
                events.getNextEvent(eventCache);

                if(eventCache.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND)
                    currentForegroundApp = eventCache.getPackageName();
            }
        }


        U.setTint(this, !blacklist.isBlocked(currentForegroundApp));
    }
}
 
開發者ID:farmerbb,項目名稱:Night-Light,代碼行數:30,代碼來源:NotificationService.java

示例11: getTopActivityAfterLM

import android.app.usage.UsageStatsManager; //導入依賴的package包/類
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
private String getTopActivityAfterLM() {
    try {
        UsageStatsManager usageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
        long milliSecs = 60 * 1000;
        Date date = new Date();
        List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, date.getTime() - milliSecs, date.getTime());
        if (queryUsageStats.size() > 0) {
            return null;
        }
        long recentTime = 0;
        String recentPkg = "";
        for (int i = 0; i < queryUsageStats.size(); i++) {
            UsageStats stats = queryUsageStats.get(i);
            if (stats.getLastTimeStamp() > recentTime) {
                recentTime = stats.getLastTimeStamp();
                recentPkg = stats.getPackageName();
            }
        }
        return recentPkg;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}
 
開發者ID:zhaozepeng,項目名稱:GrabQQPWD,代碼行數:26,代碼來源:BackgroundDetectService.java

示例12: isRunningForeground5

import android.app.usage.UsageStatsManager; //導入依賴的package包/類
/**
 *
 * 通過UsageStatsManager來判斷
 *
 * @return
 */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean isRunningForeground5() {
    if(Build.VERSION.SDK_INT >= 21){
        long ts = System.currentTimeMillis();
        UsageStatsManager usageStatsManager = (UsageStatsManager)
                getApplicationContext().getSystemService(Context.USAGE_STATS_SERVICE);
        List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(
                UsageStatsManager.INTERVAL_BEST, ts - 2000, ts);
        if (queryUsageStats == null || queryUsageStats.isEmpty()) {
            return false;
        }
        UsageStats recentStats = null;
        for (UsageStats usageStats : queryUsageStats) {
            if (recentStats == null ||
                    recentStats.getLastTimeUsed() < usageStats.getLastTimeUsed())
            {
                recentStats = usageStats;
            }
        }
        if(getPackageName().equals(recentStats.getPackageName())){
            return true;
        }
    }
    return false;
}
 
開發者ID:xzwc,項目名稱:AndroidProject,代碼行數:32,代碼來源:BaseActivity.java

示例13: getUsageStats

import android.app.usage.UsageStatsManager; //導入依賴的package包/類
@SuppressWarnings("ResourceType")
@Nullable
private SortedMap<Long, UsageStats> getUsageStats(@NonNull Context context)
        throws SecurityException {
    UsageStatsManager usm = (UsageStatsManager) context.getSystemService(USAGE_STATS_MANAGER);

    // We get usage stats for the last 30 seconds
    final long timeEnd = System.currentTimeMillis();
    final long timeBegin = timeEnd - 30 * 1000; // +30 sec.
    List<UsageStats> statsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
            timeBegin,
            timeEnd);

    if (statsList != null) {
        // Sort the stats by the last time used
        SortedMap<Long, UsageStats> statsSortedMap = new TreeMap<>();
        for (final UsageStats usageStats : statsList) {
            // Filter system decor apps
            if ("com.android.systemui".equals(usageStats.getPackageName())) {
                continue;
            }

            statsSortedMap.put(usageStats.getLastTimeUsed(), usageStats);
        }
        return statsSortedMap;
    }
    return null;
}
 
開發者ID:AChep,項目名稱:AcDisplay,代碼行數:29,代碼來源:RunningTasksLollipop.java

示例14: doInBackground

import android.app.usage.UsageStatsManager; //導入依賴的package包/類
@Override
protected List<UsageEventsItem> doInBackground(Void... params) {
    //noinspection ResourceType
    UsageStatsManager usageStatsMgr = (UsageStatsManager) getSystemService("usagestats");
    UsageEvents events = usageStatsMgr.queryEvents(mStartTime, mEndTime);
    List<UsageEventsItem> results = new ArrayList<>();
    UsageEvents.Event event = new UsageEvents.Event();
    PackageManager pm = getPackageManager();
    while (events.getNextEvent(event)) {
        UsageEventsItem item = new UsageEventsItem();
        item.pkgName = event.getPackageName();
        item.className = event.getClassName();
        item.type = event.getEventType();
        item.timeStamp = event.getTimeStamp();
        item.appName = item.pkgName;
        try {
            item.appName = pm.getApplicationInfo(item.pkgName, 0).loadLabel(pm).toString();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        results.add(item);
    }
    Collections.sort(results, new UsageEventsItem.AppNameComparator());
    return results;
}
 
開發者ID:ycdev-demo,項目名稱:LollipopExplorer,代碼行數:26,代碼來源:UsageEventsViewerActivity.java

示例15: getTopApp

import android.app.usage.UsageStatsManager; //導入依賴的package包/類
public static String getTopApp(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // 5.0及之後的方法
        UsageStatsManager usm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
        if (usm != null) {
            long now = System.currentTimeMillis();
            // 獲取40秒之內的應用程序使用狀態
            List<UsageStats> stats = usm.queryUsageStats(UsageStatsManager.INTERVAL_BEST, now - 40 * 1000, now);
            String topActivity = "";
            // 獲取最新運行的程序
            if ((stats != null) && (!stats.isEmpty())) {
                int j = 0;
                for (int i = 0; i < stats.size(); i++) {
                    if (stats.get(i).getLastTimeUsed() > stats.get(j).getLastTimeUsed()) {
                        j = i;
                    }
                }
                topActivity = stats.get(j).getPackageName();
                return topActivity;
            }
        }
    } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){ // 5.0之前的方法
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ComponentName cn = activityManager.getRunningTasks(1).get(0).topActivity;
        return cn.getPackageName();
    }
    return "Not found!";
}
 
開發者ID:Sl0v3C,項目名稱:Android_AutoSignInTool,代碼行數:28,代碼來源:Utils.java


注:本文中的android.app.usage.UsageStatsManager類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。