简体   繁体   English

Android:获取已安装应用程序的运行时间

[英]Android: get running time of installed apps

How we show all installed application with running time and get last usage of applications and show it?我们如何显示所有已安装的应用程序的运行时间并获取应用程序的最后使用情况并显示它? I get all installed application and show it, but i want to show running time of applications also.我得到所有已安装的应用程序并显示它,但我也想显示应用程序的运行时间。

Try this:尝试这个:

Add this in manifest file:在清单文件中添加:

<uses-permission
    android:name="android.permission.PACKAGE_USAGE_STATS"
    tools:ignore="ProtectedPermissions" />
<uses-permission
    android:name="android.permission.QUERY_ALL_PACKAGES"
    tools:ignore="QueryAllPackagesPermission" />

You also need to take package usage stats permission at runtime您还需要在运行时获取 package 使用统计权限

Use this function to check for the Usage Stats permission:使用此 function 检查使用统计权限:

@TargetApi(Build.VERSION_CODES.M)
public static boolean hasUsageStatsPermission(Context context) {
    boolean granted = false;
    AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), context.getPackageName());
    if (mode == AppOpsManager.MODE_DEFAULT)
        granted = (context.checkCallingOrSelfPermission(android.Manifest.permission.PACKAGE_USAGE_STATS) == PackageManager.PERMISSION_GRANTED);
    else
        granted = (mode == AppOpsManager.MODE_ALLOWED);
    return granted;
}

If you don't have the permission, use this code to take the user to your app's permission page:如果您没有权限,请使用此代码将用户带到您应用的权限页面:

startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY).setData(Uri.parse("package:" + "your package name")))

Now the actual part where you fetch the last usage time of all installed apps:现在是您获取所有已安装应用程序的最后使用时间的实际部分:

PackageManager packageManager = getPackageManager();
List<ApplicationInfo> applicationInfoList = packageManager.getInstalledApplications(0);
UsageStatsManager usageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -1);
long start = calendar.getTimeInMillis();
long end = System.currentTimeMillis();
stats = usageStatsManager.queryAndAggregateUsageStats(start, end);

for (ApplicationInfo info : applicationInfoList) {
            GetAppInfo(info);
        }

Now the GetAppInfo function:现在GetAppInfo function:

UsageStats usageStats = stats.get(info.packageName);
long lastTimeUsed;
long actualLastTimeUsed;
  if(usageStats!=null){
     lastTimeUsed = usageStats.getLastTimeUsed();
      actualLastTimeUsed = currentTime - lastTimeUsed;
  }

As you can see it's a long process to get just the last usage time of all installed apps.如您所见,仅获取所有已安装应用程序的最后使用时间是一个漫长的过程。

Note: Remember to do all this in background/worker thread rather than the UI or main thread or else it might freeze the ui注意:请记住在后台/工作线程而不是 UI 或主线程中执行所有这些操作,否则它可能会冻结 ui

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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