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


Java UsageStats.getPackageName方法代碼示例

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


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

示例1: retrieveStats

import android.app.usage.UsageStats; //導入方法依賴的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: getAppEntriesUsingUsageStats

import android.app.usage.UsageStats; //導入方法依賴的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

示例3: getTopActivityAfterLM

import android.app.usage.UsageStats; //導入方法依賴的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

示例4: getRecentAppInfo

import android.app.usage.UsageStats; //導入方法依賴的package包/類
@Deprecated
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public List<AppEntity> getRecentAppInfo(){
    List<UsageStats> usageStatsList = getUsageStatsList();
    List<AppEntity> list = new ArrayList<>();
    for (UsageStats u : usageStatsList){
        String packageName = u.getPackageName();
        ApplicationInfo applicationInfo = getAppInfo(packageName);
        //system app will not appear recent list
        //if(isSystemApp(packageName))continue;
        if (isShowSelf(packageName)) continue;
        AppEntity entity = DataHelper.getAppByPackageName(packageName);
        if (entity == null)continue;
        list.add (entity);
    }
    return list;
}
 
開發者ID:maoruibin,項目名稱:AppPlus,代碼行數:18,代碼來源:AppInfoEngine.java

示例5: getForegroundAppName

import android.app.usage.UsageStats; //導入方法依賴的package包/類
public String getForegroundAppName(Context context) {

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {

            if(needPermissionForBlocking(context)) {
                Toast.makeText(context, "Turn on Settings->Security->[Apps with usage access]", Toast.LENGTH_SHORT).show();
            }

            // intentionally using string value as Context.USAGE_STATS_SERVICE was
            // strangely only added in API 22 (LOLLIPOP_MR1)
            @SuppressWarnings("WrongConstant")
            UsageStatsManager usm = (UsageStatsManager) context.getSystemService("usagestats");
            long now = System.currentTimeMillis();
            List<UsageStats> usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, now - 1000 * 1000, now);
            if (usageStatsList != null && usageStatsList.size() > 0) {
                long time = 0;
                String name = "";
                for(UsageStats usageStats : usageStatsList) {
                    if(usageStats.getLastTimeUsed() > time) {
                        time = usageStats.getLastTimeUsed();
                        name = usageStats.getPackageName();
                    }
                }
                return name;
            }
        } else {
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            try {
                ActivityManager.RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);
                return foregroundTaskInfo.topActivity.getPackageName();
            } catch (Exception e) {
                Log.e(TAG, "can not get foreground app: " + e.getMessage(), e);
            }
        }
        return "";
    }
 
開發者ID:wxiaoguang,項目名稱:AndroidTcpdumpGui,代碼行數:37,代碼來源:CaptureManager.java

示例6: mostRecentPackage

import android.app.usage.UsageStats; //導入方法依賴的package包/類
@Nullable
private static String mostRecentPackage(List<UsageStats> stats) {
    UsageStats recentUsage = null;
    for (UsageStats currentUsage : stats) {
        String currentPackage = currentUsage.getPackageName();
        if (shouldIgnore(currentPackage)) {
            continue;
        }
        if (recentUsage == null ||
                recentUsage.getLastTimeUsed() < currentUsage.getLastTimeUsed()) {
            recentUsage = currentUsage;
        }
    }
    return recentUsage == null ? null : recentUsage.getPackageName();
}
 
開發者ID:tasomaniac,項目名稱:OpenLinkWith,代碼行數:16,代碼來源:CallerPackageExtractor.java

示例7: getRunningTasksTop

import android.app.usage.UsageStats; //導入方法依賴的package包/類
/**
 * {@inheritDoc}
 */
@Nullable
public String getRunningTasksTop(@NonNull Context context) {
    try {
        UsageStats usageStats = getUsageStatsTop(context);
        return usageStats != null ? usageStats.getPackageName() : null;
    } catch (SecurityException e) {
        Log.e(TAG, "Failed to get usage stats! Permissions denied!");
        e.printStackTrace();
    }
    return null;
}
 
開發者ID:AChep,項目名稱:AcDisplay,代碼行數:15,代碼來源:RunningTasksLollipop.java

示例8: parseUsageStats

import android.app.usage.UsageStats; //導入方法依賴的package包/類
@TargetApi(21)
    public static LollipopTaskInfo parseUsageStats(List<UsageStats> stats, LollipopTaskInfo lollipopTaskInfo) {
        UsageStats aRunner = stats.get(0);
        UsageStats bRunner = null;

        if (lollipopTaskInfo == null) {
            // setup new lollipopTaskInfo object!

            lollipopTaskInfo = new Tools.LollipopTaskInfo(aRunner.getPackageName());
        } else if (lollipopTaskInfo.packageName.equals(aRunner.getPackageName())) {
//            Tools.HangarLog("Last package same as current top, skipping! [" + lollipopTaskInfo.packageName + "]");
            return lollipopTaskInfo;
        }

        // TODO change this to keep track of all usagestats and compare timeinFg deltas
        // Will need to refactor buildTasks to manage bulk time change to db as well as
        // new runningTask.

        for (UsageStats s : stats) {
            if (s.getPackageName().equals(lollipopTaskInfo.packageName)) {
                bRunner = s;
            }
        }

        lollipopTaskInfo.lastPackageName = lollipopTaskInfo.packageName;
        lollipopTaskInfo.packageName = aRunner.getPackageName();
        if (bRunner == null) {
            Tools.HangarLog("Couldn't find previous task [" + lollipopTaskInfo.packageName + "]");
        } else {
            lollipopTaskInfo.timeInFGDelta = (lollipopTaskInfo.timeInFG > 0) ? bRunner.getTotalTimeInForeground() - lollipopTaskInfo.timeInFG : 0;
        }
        lollipopTaskInfo.timeInFG = aRunner.getTotalTimeInForeground();

        Tools.HangarLog("New [" + lollipopTaskInfo.packageName + "] old [" + lollipopTaskInfo.lastPackageName + "] old FG delta: " + lollipopTaskInfo.timeInFGDelta);

        return lollipopTaskInfo;
    }
 
開發者ID:corcoran,項目名稱:Hangar,代碼行數:38,代碼來源:Tools.java

示例9: buildLBaseTasks

import android.app.usage.UsageStats; //導入方法依賴的package包/類
@TargetApi(21)
protected void buildLBaseTasks() {
    Context context = getApplicationContext();
    List<UsageStats> stats = Tools.getUsageStats(context);

    for (UsageStats task : stats) {
        String taskPackage = task.getPackageName();
        String taskClass = getClassName(context, taskPackage);
        Tools.HangarLog("buildLBaseTask: package: " + taskPackage + " class: " + taskClass);
        buildTaskInfo(taskClass, taskPackage);
    }
}
 
開發者ID:corcoran,項目名稱:Hangar,代碼行數:13,代碼來源:WatchfulService.java

示例10: doInBackground

import android.app.usage.UsageStats; //導入方法依賴的package包/類
@Override
protected List<AppsUsageItem> doInBackground(Void... params) {
    //noinspection ResourceType
    UsageStatsManager usageStatsMgr = (UsageStatsManager) getSystemService("usagestats");
    Map<String, UsageStats> usagesList = usageStatsMgr.queryAndAggregateUsageStats(
            mStartTime, mEndTime);
    AppLogger.i(TAG, "usages stat list size: " + usagesList.size());
    List<AppsUsageItem> results = new ArrayList<AppsUsageItem>();
    PackageManager pm = getPackageManager();
    for (UsageStats usage : usagesList.values()) {
        AppsUsageItem item = new AppsUsageItem();
        item.pkgName = usage.getPackageName();
        item.lastStartup = usage.getLastTimeUsed();
        item.lastStartupStr = getString(R.string.apps_usage_last_startup,
                DateTimeUtils.generateFileName(item.lastStartup));
        item.fgTime = usage.getTotalTimeInForeground();
        item.fgTimeStr = getString(R.string.apps_usage_fg_time_used,
                DateTimeUtils.getReadableTimeUsage(item.fgTime));
        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 AppsUsageItem.AppNameComparator());
    return results;
}
 
開發者ID:ycdev-demo,項目名稱:LollipopExplorer,代碼行數:30,代碼來源:AppsUsageViewerActivity.java

示例11: getForegroundProcessName

import android.app.usage.UsageStats; //導入方法依賴的package包/類
/**
 * 獲取前台線程包名
 * <p>當不是查看當前App,且SDK大於21時,
 * 需添加權限 {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>}</p>
 *
 * @return 前台應用包名
 */
public static String getForegroundProcessName() {
    ActivityManager manager = (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> pInfo = manager.getRunningAppProcesses();
    if (pInfo != null && pInfo.size() != 0) {
        for (ActivityManager.RunningAppProcessInfo aInfo : pInfo) {
            if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return aInfo.processName;
            }
        }
    }
    if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
        PackageManager packageManager = Utils.getApp().getPackageManager();
        Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        System.out.println(list);
        if (list.size() > 0) {// 有"有權查看使用權限的應用"選項
            try {
                ApplicationInfo info = packageManager.getApplicationInfo(Utils.getApp().getPackageName(), 0);
                AppOpsManager aom = (AppOpsManager) Utils.getApp().getSystemService(Context.APP_OPS_SERVICE);
                if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) {
                    Utils.getApp().startActivity(intent);
                }
                if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) {
                    LogUtils.d("getForegroundApp", "沒有打開\"有權查看使用權限的應用\"選項");
                    return null;
                }
                UsageStatsManager usageStatsManager = (UsageStatsManager) Utils.getApp().getSystemService(Context.USAGE_STATS_SERVICE);
                long endTime = System.currentTimeMillis();
                long beginTime = endTime - 86400000 * 7;
                List<UsageStats> usageStatses = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, beginTime, endTime);
                if (usageStatses == null || usageStatses.isEmpty()) return null;
                UsageStats recentStats = null;
                for (UsageStats usageStats : usageStatses) {
                    if (recentStats == null || usageStats.getLastTimeUsed() > recentStats.getLastTimeUsed()) {
                        recentStats = usageStats;
                    }
                }
                return recentStats == null ? null : recentStats.getPackageName();
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            Log.d("ProcessUtils", "getForegroundProcessName() called" + ": 無\"有權查看使用權限的應用\"選項");
        }
    }
    return null;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:55,代碼來源:ProcessUtils.java

示例12: getForegroundProcessName

import android.app.usage.UsageStats; //導入方法依賴的package包/類
/**
 * 獲取前台線程包名
 * <p>當不是查看當前App,且SDK大於21時,
 * 需添加權限 {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>}</p>
 *
 * @return 前台應用包名
 */
public static String getForegroundProcessName() {
    ActivityManager manager = (ActivityManager) Utils.getContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> pInfo = manager.getRunningAppProcesses();
    if (pInfo != null && pInfo.size() != 0) {
        for (ActivityManager.RunningAppProcessInfo aInfo : pInfo) {
            if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return aInfo.processName;
            }
        }
    }
    if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
        PackageManager packageManager = Utils.getContext().getPackageManager();
        Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        System.out.println(list);
        if (list.size() > 0) {// 有"有權查看使用權限的應用"選項
            try {
                ApplicationInfo info = packageManager.getApplicationInfo(Utils.getContext().getPackageName(), 0);
                AppOpsManager aom = (AppOpsManager) Utils.getContext().getSystemService(Context.APP_OPS_SERVICE);
                if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) {
                    Utils.getContext().startActivity(intent);
                }
                if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) {
                    LogUtils.d("getForegroundApp", "沒有打開\"有權查看使用權限的應用\"選項");
                    return null;
                }
                UsageStatsManager usageStatsManager = (UsageStatsManager) Utils.getContext().getSystemService(Context.USAGE_STATS_SERVICE);
                long endTime = System.currentTimeMillis();
                long beginTime = endTime - 86400000 * 7;
                List<UsageStats> usageStatses = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, beginTime, endTime);
                if (usageStatses == null || usageStatses.isEmpty()) return null;
                UsageStats recentStats = null;
                for (UsageStats usageStats : usageStatses) {
                    if (recentStats == null || usageStats.getLastTimeUsed() > recentStats.getLastTimeUsed()) {
                        recentStats = usageStats;
                    }
                }
                return recentStats == null ? null : recentStats.getPackageName();
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            LogUtils.d("getForegroundApp", "無\"有權查看使用權限的應用\"選項");
        }
    }
    return null;
}
 
開發者ID:hoangkien0705,項目名稱:Android-UtilCode,代碼行數:55,代碼來源:ProcessUtils.java

示例13: getForegroundProcessName

import android.app.usage.UsageStats; //導入方法依賴的package包/類
/**
 * 獲取前台線程包名
 * <p>當不是查看當前App,且SDK大於21時,
 * 需添加權限 {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>}</p>
 *
 * @return 前台應用包名
 */
public static String getForegroundProcessName() {
    ActivityManager manager = (ActivityManager) Utils.getContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> pInfo = manager.getRunningAppProcesses();
    if (pInfo != null && pInfo.size() != 0) {
        for (ActivityManager.RunningAppProcessInfo aInfo : pInfo) {
            if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return aInfo.processName;
            }
        }
    }
    if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
        PackageManager packageManager = Utils.getContext().getPackageManager();
        Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        System.out.println(list);
        if (list.size() > 0) {// 有"有權查看使用權限的應用"選項
            try {
                ApplicationInfo info = packageManager.getApplicationInfo(Utils.getContext().getPackageName(), 0);
                AppOpsManager aom = (AppOpsManager) Utils.getContext().getSystemService(Context.APP_OPS_SERVICE);
                if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) {
                    Utils.getContext().startActivity(intent);
                }
                if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) {
                    LogUtils.d("getForegroundApp", "沒有打開\"有權查看使用權限的應用\"選項");
                    return null;
                }
                UsageStatsManager usageStatsManager = (UsageStatsManager) Utils.getContext().getSystemService(Context.USAGE_STATS_SERVICE);
                long endTime = System.currentTimeMillis();
                long beginTime = endTime - 86400000 * 7;
                List<UsageStats> usageStatses = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, beginTime, endTime);
                if (usageStatses == null || usageStatses.isEmpty()) return null;
                UsageStats recentStats = null;
                for (UsageStats usageStats : usageStatses) {
                    if (recentStats == null || usageStats.getLastTimeUsed() > recentStats.getLastTimeUsed()) {
                        recentStats = usageStats;
                    }
                }
                return recentStats == null ? null : recentStats.getPackageName();
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            Log.d("ProcessUtils", "getForegroundProcessName() called" + ": 無\"有權查看使用權限的應用\"選項");
        }
    }
    return null;
}
 
開發者ID:Wilshion,項目名稱:HeadlineNews,代碼行數:55,代碼來源:ProcessUtils.java

示例14: getForegroundProcessName

import android.app.usage.UsageStats; //導入方法依賴的package包/類
/**
 * 獲取前台線程包名
 * <p>當不是查看當前 App,且 SDK 大於 21 時,
 * 需添加權限
 * {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />}</p>
 *
 * @return 前台應用包名
 */
public static String getForegroundProcessName() {
    ActivityManager manager =
            (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
    if (manager == null) return null;
    List<ActivityManager.RunningAppProcessInfo> pInfo = manager.getRunningAppProcesses();
    if (pInfo != null && pInfo.size() > 0) {
        for (ActivityManager.RunningAppProcessInfo aInfo : pInfo) {
            if (aInfo.importance
                    == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return aInfo.processName;
            }
        }
    }
    if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
        PackageManager packageManager = Utils.getApp().getPackageManager();
        Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
        List<ResolveInfo> list =
                packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        Log.i("ProcessUtils", list.toString());
        if (list.size() <= 0) {
            Log.i("ProcessUtils",
                    "getForegroundProcessName() called" + ": 無\"有權查看使用權限的應用\"選項");
            return null;
        }
        try {// 有"有權查看使用權限的應用"選項
            ApplicationInfo info =
                    packageManager.getApplicationInfo(Utils.getApp().getPackageName(), 0);
            AppOpsManager aom =
                    (AppOpsManager) Utils.getApp().getSystemService(Context.APP_OPS_SERVICE);
            if (aom != null) {
                if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
                        info.uid,
                        info.packageName) != AppOpsManager.MODE_ALLOWED) {
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Utils.getApp().startActivity(intent);
                }
                if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
                        info.uid,
                        info.packageName) != AppOpsManager.MODE_ALLOWED) {
                    Log.i("ProcessUtils", "沒有打開\"有權查看使用權限的應用\"選項");
                    return null;
                }
            }
            UsageStatsManager usageStatsManager = (UsageStatsManager) Utils.getApp()
                    .getSystemService(Context.USAGE_STATS_SERVICE);
            List<UsageStats> usageStatsList = null;
            if (usageStatsManager != null) {
                long endTime = System.currentTimeMillis();
                long beginTime = endTime - 86400000 * 7;
                usageStatsList = usageStatsManager
                        .queryUsageStats(UsageStatsManager.INTERVAL_BEST,
                                beginTime, endTime);
            }
            if (usageStatsList == null || usageStatsList.isEmpty()) return null;
            UsageStats recentStats = null;
            for (UsageStats usageStats : usageStatsList) {
                if (recentStats == null
                        || usageStats.getLastTimeUsed() > recentStats.getLastTimeUsed()) {
                    recentStats = usageStats;
                }
            }
            return recentStats == null ? null : recentStats.getPackageName();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
    return null;
}
 
開發者ID:Blankj,項目名稱:AndroidUtilCode,代碼行數:77,代碼來源:ProcessUtils.java

示例15: getRunningTasksNougat

import android.app.usage.UsageStats; //導入方法依賴的package包/類
@TargetApi(21)
private List<Task> getRunningTasksNougat() {
    final List<Task> tasks = new ArrayList<>();

    AppOpsManager appOps = (AppOpsManager) mContext
            .getSystemService(Context.APP_OPS_SERVICE);
    int mode = appOps.checkOpNoThrow("android:get_usage_stats",
            android.os.Process.myUid(), mContext.getPackageName());
    boolean granted = mode == AppOpsManager.MODE_ALLOWED;

    if (!granted) return tasks;

    final List<UsageStats> list = UStats.getUsageStatsList(mContext);

    for (UsageStats stats : list) {
        String packageName = stats.getPackageName();
        /** Exclude the app itself from the list */
        if (packageName.equals(BuildConfig.APPLICATION_ID)) continue;

        PackageInfo packageInfo = getPackageInfo(packageName, 0);

        if (packageInfo == null) continue;

        /** Remove system apps if necessary */
        if (isSystemApp(packageInfo) && SettingsUtils.isSystemAppsHidden(mContext)){
            continue;
        }

        /** Remove apps without label */
        String appLabel = packageInfo.applicationInfo.loadLabel(mPackageManager).toString();
        if (appLabel.isEmpty()) continue;

        int uid = packageInfo.applicationInfo.uid;
        Task task = getTaskByUid(tasks, uid);

        if (task == null) {
            task = new Task(uid, packageInfo.applicationInfo.processName);
            task.setPackageInfo(packageInfo);
            task.setLabel(appLabel);
            task.setIsAutoStart(isAutoStartApp(packageName));
            task.setHasBackgroundService(hasBackgroundServices(packageName));
            tasks.add(task);
        }
    }

    // Dirty quick sorting
    Collections.sort(tasks, new Comparator<Task>() {
        @Override
        public int compare(Task t1, Task t2) {
            return t1.getLabel().compareTo(t2.getLabel());
        }
    });

    return tasks;
}
 
開發者ID:greenhub-project,項目名稱:batteryhub,代碼行數:56,代碼來源:TaskController.java


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