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


Java UsageStats.getLastTimeUsed方法代碼示例

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


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

示例1: isRunningForeground5

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

示例2: getPackageStartTime

import android.app.usage.UsageStats; //導入方法依賴的package包/類
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static long getPackageStartTime(Context context,String packageName){
    long cTime = System.currentTimeMillis();
    List<UsageStats> result = getPackageUsageStats(context);
    if (result != null) {
        for (UsageStats stat : result) {
            if (packageName.equalsIgnoreCase(stat.getPackageName())) {
                if (cTime - stat.getLastTimeUsed() < 5) {
                    return stat.getLastTimeUsed();
                }
            }
        }
    }
    return 0;
}
 
開發者ID:miLLlulei,項目名稱:Accessibility,代碼行數:16,代碼來源:PackageUsageHelper.java

示例3: queryUsageStats

import android.app.usage.UsageStats; //導入方法依賴的package包/類
/**
 * 方法4:通過使用UsageStatsManager獲取,此方法是ndroid5.0A之後提供的API
 * 必須:
 * 1. 此方法隻在android5.0以上有效
 * 2. AndroidManifest中加入此權限<uses-permission xmlns:tools="http://schemas.android.com/tools" android:name="android.permission.PACKAGE_USAGE_STATS"
 * tools:ignore="ProtectedPermissions" />
 * 3. 打開手機設置,點擊安全-高級,在有權查看使用情況的應用中,為這個App打上勾
 *
 * @param context     上下文參數
 * @param packageName 需要檢查是否位於棧頂的App的包名
 * @return
 */

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static boolean queryUsageStats(Context context, String packageName) {
    class RecentUseComparator implements Comparator<UsageStats> {
        @Override
        public int compare(UsageStats lhs, UsageStats rhs) {
            return (lhs.getLastTimeUsed() > rhs.getLastTimeUsed()) ? -1 : (lhs.getLastTimeUsed() == rhs.getLastTimeUsed()) ? 0 : 1;
        }
    }
    RecentUseComparator mRecentComp = new RecentUseComparator();
    long ts = System.currentTimeMillis();
    UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService("usagestats");
    List<UsageStats> usageStats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, ts - 1000 * 10, ts);
    if (usageStats == null || usageStats.size() == 0) {
        if (HavaPermissionForTest(context) == false) {
            Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
            Toast.makeText(context, "權限不夠\n請打開手機設置,點擊安全-高級,在有權查看使用情況的應用中,為這個App打上勾", Toast.LENGTH_SHORT).show();
        }
        return false;
    }
    Collections.sort(usageStats, mRecentComp);
    String currentTopPackage = usageStats.get(0).getPackageName();
    if (currentTopPackage.equals(packageName)) {
        return true;
    } else {
        return false;
    }
}
 
開發者ID:wenmingvs,項目名稱:AndroidProcess,代碼行數:43,代碼來源:BackgroundUtil.java

示例4: 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

示例5: 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

示例6: 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

示例7: 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

示例8: 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

示例9: 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

示例10: 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

示例11: compare

import android.app.usage.UsageStats; //導入方法依賴的package包/類
@Override
public final int compare(UsageStats a, UsageStats b) {
    return (int)(b.getLastTimeUsed() - a.getLastTimeUsed());
}
 
開發者ID:spate141,項目名稱:App-Monitor,代碼行數:5,代碼來源:LastTimeUsedComparator.java

示例12: compare

import android.app.usage.UsageStats; //導入方法依賴的package包/類
@Override
public int compare(UsageStats t1, UsageStats t2) {
    o1 = t1.getLastTimeUsed();
    o2 = t2.getLastTimeUsed();
    return o2.compareTo(o1);
}
 
開發者ID:corcoran,項目名稱:Hangar,代碼行數:7,代碼來源:Tools.java


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