Java Code Examples for android.app.usage.UsageStats#getLastTimeUsed()
The following examples show how to use
android.app.usage.UsageStats#getLastTimeUsed() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BackgroundUtil.java From AndroidProcess with Apache License 2.0 | 6 votes |
/** * 方法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; } }
Example 2
Source File: AppUtils.java From Common with Apache License 2.0 | 4 votes |
private static String getForegroundProcessName(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> pInfo = am.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 pm = context.getPackageManager(); Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); Log.i("ProcessUtils", list.toString()); if (list.size() <= 0) { Log.i("ProcessUtils", "getForegroundProcessName: noun of access to usage information."); return ""; } try { // Access to usage information. ApplicationInfo info = pm.getApplicationInfo(context.getPackageName(), 0); AppOpsManager aom = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) { Log.i("ProcessUtils", "getForegroundProcessName: refuse to device usage stats."); return ""; } UsageStatsManager usageStatsManager = (UsageStatsManager) context .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 ""; }
Example 3
Source File: ProcessUtils.java From Android-UtilCode with Apache License 2.0 | 4 votes |
/** * 获取前台线程包名 * <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; }
Example 4
Source File: ProcessUtils.java From AndroidUtilCode with Apache License 2.0 | 4 votes |
/** * Return the foreground process name. * <p>Target APIs greater than 21 must hold * {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />}</p> * * @return the foreground process name */ public static String getForegroundProcessName() { ActivityManager am = (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE); //noinspection ConstantConditions List<ActivityManager.RunningAppProcessInfo> pInfo = am.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 pm = Utils.getApp().getPackageManager(); Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); Log.i("ProcessUtils", list.toString()); if (list.size() <= 0) { Log.i("ProcessUtils", "getForegroundProcessName: noun of access to usage information."); return ""; } try {// Access to usage information. ApplicationInfo info = pm.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) { 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", "getForegroundProcessName: refuse to device usage stats."); return ""; } 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 ""; 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 ""; }
Example 5
Source File: Tools.java From Hangar with GNU General Public License v3.0 | 4 votes |
@Override public int compare(UsageStats t1, UsageStats t2) { o1 = t1.getLastTimeUsed(); o2 = t2.getLastTimeUsed(); return o2.compareTo(o1); }