本文整理汇总了Java中android.provider.Settings.ACTION_USAGE_ACCESS_SETTINGS属性的典型用法代码示例。如果您正苦于以下问题:Java Settings.ACTION_USAGE_ACCESS_SETTINGS属性的具体用法?Java Settings.ACTION_USAGE_ACCESS_SETTINGS怎么用?Java Settings.ACTION_USAGE_ACCESS_SETTINGS使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类android.provider.Settings
的用法示例。
在下文中一共展示了Settings.ACTION_USAGE_ACCESS_SETTINGS属性的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: openUsageSettings
public void openUsageSettings(View view){
try {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
}catch (Throwable e){
Toast.makeText(this,"无法打开系统设置,请手动设置",Toast.LENGTH_SHORT).show();
}
}
示例2: getForegroundProcessName
/**
* 获取前台线程包名
* <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;
}
示例3: getForegroundProcessName
/**
* 获取前台线程包名
* <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;
}
示例4: requestPermission
public void requestPermission(Context context) {
Intent intent = new Intent(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
示例5: getForegroundProcessName
/**
* 获取前台线程包名
* <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;
}