frameworks 判断 android.permission.FOREGROUND_SERVICE 的地方
时间: 2023-09-24 09:11:17 浏览: 47
在 Android 应用程序中,可以使用以下两种方式来判断是否具有 FOREGROUND_SERVICE 权限:
1. 使用 ContextCompat.checkSelfPermission() 方法来检查应用程序是否具有 FOREGROUND_SERVICE 权限,代码如下:
```
if (ContextCompat.checkSelfPermission(context, Manifest.permission.FOREGROUND_SERVICE) == PackageManager.PERMISSION_GRANTED) {
// 应用程序具有 FOREGROUND_SERVICE 权限
} else {
// 应用程序不具有 FOREGROUND_SERVICE 权限
}
```
2. 使用 ActivityCompat.requestPermissions() 方法来请求 FOREGROUND_SERVICE 权限,代码如下:
```
ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.FOREGROUND_SERVICE }, requestCode);
```
在请求权限后,应用程序需要实现 onRequestPermissionsResult() 方法来处理权限请求结果,并根据结果来执行相应的操作。
相关问题
android 9 原生frameworks 判断 android.permission.FOREGROUND_SERVICE
Android 9 原生 Framework 中判断 `android.permission.FOREGROUND_SERVICE` 权限的方式如下:
在应用程序的代码中,可以使用 `ContextCompat.checkSelfPermission()` 方法来检查某个权限是否已经被授予。示例代码如下:
```java
if (ContextCompat.checkSelfPermission(this, Manifest.permission.FOREGROUND_SERVICE)
== PackageManager.PERMISSION_GRANTED) {
// 权限已经被授予
// TODO: 执行相应的操作
} else {
// 权限还没有被授予,需要向用户请求授权
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.FOREGROUND_SERVICE},
PERMISSION_REQUEST_FOREGROUND_SERVICE);
}
```
其中,`PERMISSION_REQUEST_FOREGROUND_SERVICE` 是自定义的请求码,用于在 `onRequestPermissionsResult()` 回调方法中识别对应的权限请求。如果用户拒绝了权限请求,可以在 `onRequestPermissionsResult()` 方法中处理相应的逻辑,例如显示一个提示对话框或者禁用某些功能。
android 原生frameworks 判断 android.permission.FOREGROUND_SERVICE
在 Android 原生 Framework 中,可以使用以下方式来判断是否具有 FOREGROUND_SERVICE 权限:
1. 在 PackageManagerService.java 中,可以找到 checkPermission() 方法,该方法用于检查应用程序是否具有指定的权限。在该方法中,可以查找 FOREGROUND_SERVICE 权限并返回相应的结果,代码如下:
```
private int checkPermission(String perm, int uid, int callingPid, int callingUid) {
// ...
if (perm.equals(Manifest.permission.FOREGROUND_SERVICE)) {
if (mAppOpsService.noteOperation(AppOpsManager.OP_RUN_FOREGROUND, uid, packageName) == AppOpsManager.MODE_ALLOWED) {
granted = PackageManager.PERMISSION_GRANTED;
}
}
// ...
return granted;
}
```
在该方法中,通过调用 AppOpsManager.noteOperation() 方法来检查应用程序是否具有 FOREGROUND_SERVICE 权限。如果应用程序具有该权限,则返回 PackageManager.PERMISSION_GRANTED;否则返回 PackageManager.PERMISSION_DENIED。
2. 在 ActivityManagerService.java 中,可以找到 checkForegroundServicePermission() 方法,该方法用于检查应用程序是否具有 FOREGROUND_SERVICE 权限以及是否可以启动前台服务。在该方法中,可以先检查应用程序是否具有 FOREGROUND_SERVICE 权限,如果有,则再检查应用程序是否可以启动前台服务,代码如下:
```
private int checkForegroundServicePermission(int uid, String packageName, int callingPid, int callingUid, int startId, boolean lastStartForeground) {
// ...
if (checkPermission(Manifest.permission.FOREGROUND_SERVICE, uid, callingPid, callingUid) != PackageManager.PERMISSION_GRANTED) {
Slog.w(TAG, "startForegroundService not allowed due to lack of FOREGROUND_SERVICE permission");
return START_NOT_STICKY;
}
final long origId = Binder.clearCallingIdentity();
boolean fgServiceAllowed = false;
try {
fgServiceAllowed = mBatteryStatsService.noteStartForeground(packageName, uid, lastStartForeground);
} catch (RemoteException e) {
Slog.w(TAG, "Failed to note battery stats for startForegroundService", e);
} finally {
Binder.restoreCallingIdentity(origId);
}
if (!fgServiceAllowed) {
Slog.w(TAG, "startForegroundService not allowed due to battery stats");
return START_NOT_STICKY;
}
// ...
return START_SUCCESS;
}
```
在该方法中,首先调用 checkPermission() 方法来检查应用程序是否具有 FOREGROUND_SERVICE 权限。如果应用程序没有该权限,则返回 START_NOT_STICKY。如果应用程序具有该权限,则调用 BatteryStatsService.noteStartForeground() 方法来检查应用程序是否可以启动前台服务。如果可以,则返回 START_SUCCESS;否则返回 START_NOT_STICKY。