Skip to content

Commit a7f60d5

Browse files
committed
修复没有在清单文件中注册 foregroundServiceType 属性导致的异常
1 parent ffbbbf8 commit a7f60d5

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
* 项目地址:[Github](https://github.com/getActivity/Logcat)
66

7-
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/Logcat/releases/download/11.85/Logcat.apk)
7+
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/Logcat/releases/download/11.86/Logcat.apk)
88

99
![](picture/demo_code.png)
1010

@@ -51,7 +51,7 @@ dependencyResolutionManagement {
5151
```groovy
5252
dependencies {
5353
// 日志调试框架:https://github.com/getActivity/Logcat
54-
debugImplementation 'com.github.getActivity:Logcat:11.85'
54+
debugImplementation 'com.github.getActivity:Logcat:11.86'
5555
}
5656
```
5757

@@ -66,6 +66,18 @@ android.enableJetifier = true
6666

6767
* 如果项目是基于 **Support** 包则不需要加入此配置
6868

69+
#### compileSdk 版本要求
70+
71+
* 如果项目的 `compileSdkVersion` 小于 29,则需要先升级成 29
72+
73+
```groovy
74+
android {
75+
compileSdkVersion 29
76+
}
77+
```
78+
79+
* 如果项目的 `compileSdkVersion` 大于等于 29,则不需要修改此配置
80+
6981
#### 使用方式
7082

7183
* 无需调用,直接运行,然后授予悬浮窗权限即可

common.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
minSdk 16
99
// Android 版本适配指南:https://github.com/getActivity/AndroidVersionAdapter
1010
targetSdk 34
11-
versionCode 1185
12-
versionName "11.85"
11+
versionCode 1186
12+
versionName "11.86"
1313
}
1414

1515
// 支持 Java JDK 8

library/src/main/AndroidManifest.xml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@
1414
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
1515

1616
<!-- 读取其他应用日志权限 -->
17-
<uses-permission android:name="android.permission.READ_LOGS" tools:ignore="ProtectedPermissions" />
17+
<uses-permission
18+
android:name="android.permission.READ_LOGS"
19+
tools:ignore="ProtectedPermissions" />
20+
21+
<!-- 前台 Service 特殊用途权限 -->
22+
<!-- java.lang.SecurityException:
23+
Starting FGS with type specialUse callerApp=ProcessRecord
24+
targetSDK=34 requires permissions: all of the permissions allOf=true
25+
[android.permission.FOREGROUND_SERVICE_SPECIAL_USE] -->
26+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
1827

1928
<application>
2029

@@ -31,7 +40,20 @@
3140
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
3241
android:windowSoftInputMode="stateHidden" />
3342

34-
<service android:name=".LogcatService" />
43+
<!-- 适配 targetSdk 34:https://developer.android.google.cn/about/versions/14/changes/fgs-types-required?hl=zh-cn -->
44+
<!-- java.lang.RuntimeException:
45+
Unable to start service com.hjq.logcat.LogcatService with Intent:
46+
android.app.MissingForegroundServiceTypeException:
47+
Starting FGS without a type callerApp=ProcessRecord targetSDK=34 -->
48+
<!-- 加上 foregroundServiceType 属性后,compileSdkVersion 需要 29 及以上的版本才能编译通过 -->
49+
<service
50+
android:name=".LogcatService"
51+
android:foregroundServiceType="specialUse">
52+
53+
<property
54+
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
55+
android:value="Obtain the debugging logs of the current application" />
56+
</service>
3557

3658
</application>
3759

library/src/main/java/com/hjq/logcat/LogcatService.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
import android.content.Context;
99
import android.content.Intent;
1010
import android.content.pm.PackageManager;
11-
import android.content.pm.ServiceInfo;
1211
import android.graphics.BitmapFactory;
13-
import android.os.Build;
1412
import android.os.Build.VERSION;
1513
import android.os.Build.VERSION_CODES;
1614
import android.os.IBinder;
@@ -92,12 +90,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
9290
}
9391

9492
// 将服务和通知绑定在一起,成为前台服务
95-
if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE &&
96-
VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
97-
startForeground(BACKUP_SERVICE_NOTIFICATION_ID, builder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
98-
} else {
99-
startForeground(BACKUP_SERVICE_NOTIFICATION_ID, builder.build());
100-
}
93+
startForeground(BACKUP_SERVICE_NOTIFICATION_ID, builder.build());
10194
return super.onStartCommand(intent, flags, startId);
10295
}
10396

0 commit comments

Comments
 (0)