Skip to content

Commit

Permalink
修复没有在清单文件中注册 foregroundServiceType 属性导致的异常
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Mar 10, 2024
1 parent ffbbbf8 commit a7f60d5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

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

![](picture/demo_code.png)

Expand Down Expand Up @@ -51,7 +51,7 @@ dependencyResolutionManagement {
```groovy
dependencies {
// 日志调试框架:https://github.com/getActivity/Logcat
debugImplementation 'com.github.getActivity:Logcat:11.85'
debugImplementation 'com.github.getActivity:Logcat:11.86'
}
```

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

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

#### compileSdk 版本要求

* 如果项目的 `compileSdkVersion` 小于 29,则需要先升级成 29

```groovy
android {
compileSdkVersion 29
}
```

* 如果项目的 `compileSdkVersion` 大于等于 29,则不需要修改此配置

#### 使用方式

* 无需调用,直接运行,然后授予悬浮窗权限即可
Expand Down
4 changes: 2 additions & 2 deletions common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
minSdk 16
// Android 版本适配指南:https://github.com/getActivity/AndroidVersionAdapter
targetSdk 34
versionCode 1185
versionName "11.85"
versionCode 1186
versionName "11.86"
}

// 支持 Java JDK 8
Expand Down
26 changes: 24 additions & 2 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

<!-- 读取其他应用日志权限 -->
<uses-permission android:name="android.permission.READ_LOGS" tools:ignore="ProtectedPermissions" />
<uses-permission
android:name="android.permission.READ_LOGS"
tools:ignore="ProtectedPermissions" />

<!-- 前台 Service 特殊用途权限 -->
<!-- java.lang.SecurityException:
Starting FGS with type specialUse callerApp=ProcessRecord
targetSDK=34 requires permissions: all of the permissions allOf=true
[android.permission.FOREGROUND_SERVICE_SPECIAL_USE] -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />

<application>

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

<service android:name=".LogcatService" />
<!-- 适配 targetSdk 34:https://developer.android.google.cn/about/versions/14/changes/fgs-types-required?hl=zh-cn -->
<!-- java.lang.RuntimeException:
Unable to start service com.hjq.logcat.LogcatService with Intent:
android.app.MissingForegroundServiceTypeException:
Starting FGS without a type callerApp=ProcessRecord targetSDK=34 -->
<!-- 加上 foregroundServiceType 属性后,compileSdkVersion 需要 29 及以上的版本才能编译通过 -->
<service
android:name=".LogcatService"
android:foregroundServiceType="specialUse">

<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="Obtain the debugging logs of the current application" />
</service>

</application>

Expand Down
9 changes: 1 addition & 8 deletions library/src/main/java/com/hjq/logcat/LogcatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.IBinder;
Expand Down Expand Up @@ -92,12 +90,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
}

// 将服务和通知绑定在一起,成为前台服务
if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE &&
VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
startForeground(BACKUP_SERVICE_NOTIFICATION_ID, builder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
} else {
startForeground(BACKUP_SERVICE_NOTIFICATION_ID, builder.build());
}
startForeground(BACKUP_SERVICE_NOTIFICATION_ID, builder.build());
return super.onStartCommand(intent, flags, startId);
}

Expand Down

0 comments on commit a7f60d5

Please sign in to comment.