我尝试使用使用情况统计信息,但在成功检索已安装的应用列表后,我尝试查询使用情况统计信息,但得到0值。
查看StackOverflow后,我发现在PACKAGE_USAGE_STATS之上还有一个额外的权限:OPSTR_GET_USAGE_STATS
当我查看此权限时,我没有。
为什么PACKAGE_USAGE_STATS不足够?
是否存在以编程方式设置此权限的方法?
否则,我不喜欢将用户重定向到设置的想法:这不是提示用户在UI中允许此权限的方法吗?
以下是我的代码摘录:
清单
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
java代码:
void getInstalledApp() {
List<String> installedApps = getInstalledAppList();
if(!checkForPermission())
Log.i("PERM", "false");
List<UsageStats> usageStats =
usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_YEARLY,
getStartTime(), System.currentTimeMillis());
List<UsageStats> stats = new ArrayList<>();
}
private List<String> getInstalledAppList(){
List<ApplicationInfo> infos = packageManager.getInstalledApplications(flags);
List<String> installedApps = new ArrayList<>();
for (ApplicationInfo info : infos){
installedApps.add(info.packageName);
}
return installedApps;
}
private long getStartTime() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -1);
return calendar.getTimeInMillis();
}
private boolean checkForPermission() {
AppOpsManager appOps = (AppOpsManager) activity.getSystemService(Context.APP_OPS_SERVICE);
int mode = appOps.checkOpNoThrow(OPSTR_GET_USAGE_STATS, myUid(), activity.getPackageName());
return mode == MODE_ALLOWED;
}
答案 0 :(得分:0)
如果未授权,请检查此方法以询问用户的权限:
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("message");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
sessionLocaleResolver.setDefaultLocale(Locale.ENGLISH);
return sessionLocaleResolver;
}