Skip to content

Commit 01a3613

Browse files
committed
优化 Android 14 前台 Service 服务类型新特性
1 parent ad15e32 commit 01a3613

File tree

6 files changed

+42
-27
lines changed

6 files changed

+42
-27
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.6/Logcat.apk)
7+
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/Logcat/releases/download/11.8/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.6'
54+
debugImplementation 'com.github.getActivity:Logcat:11.8'
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
* 无需调用,直接运行,然后授予悬浮窗权限即可

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId "com.hjq.logcat.demo"
88
minSdk 16
99
targetSdk 34
10-
versionCode 1160
11-
versionName "11.6"
10+
versionCode 1180
11+
versionName "11.8"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ plugins {
22
id 'com.android.application' version '7.1.0' apply false
33
}
44

5-
task clean(type: Delete) {
5+
tasks.register('clean', Delete) {
66
delete rootProject.buildDir
77
}

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99

1010
defaultConfig {
1111
minSdkVersion 16
12-
versionCode 1160
13-
versionName "11.6"
12+
versionCode 1180
13+
versionName "11.8"
1414
}
1515

1616
lintOptions {

library/src/main/AndroidManifest.xml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@
1010
<!-- 前台 Service 权限 -->
1111
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
1212

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

2016
<!-- 读取其他应用日志权限 -->
2117
<uses-permission android:name="android.permission.READ_LOGS" tools:ignore="ProtectedPermissions" />
@@ -40,9 +36,8 @@
4036
Unable to start service com.hjq.logcat.LogcatService with Intent:
4137
android.app.MissingForegroundServiceTypeException:
4238
Starting FGS without a type callerApp=ProcessRecord targetSDK=34 -->
43-
<service android:name=".LogcatService" android:foregroundServiceType="specialUse">
44-
<property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE" android:value="foo"/>
45-
</service>
39+
<!-- 加上 foregroundServiceType 属性后,compileSdkVersion 需要 29 及以上的版本才能编译通过 -->
40+
<service android:name=".LogcatService" android:foregroundServiceType="dataSync" />
4641

4742
</application>
4843

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package com.hjq.logcat;
22

3-
import android.app.Notification;
3+
import android.app.Notification.Builder;
44
import android.app.NotificationChannel;
55
import android.app.NotificationManager;
66
import android.app.PendingIntent;
77
import android.app.Service;
88
import android.content.Context;
99
import android.content.Intent;
1010
import android.content.pm.PackageManager;
11+
import android.content.pm.ServiceInfo;
1112
import android.graphics.BitmapFactory;
1213
import android.os.Build;
14+
import android.os.Build.VERSION;
15+
import android.os.Build.VERSION_CODES;
1316
import android.os.IBinder;
1417
import android.text.TextUtils;
1518

@@ -32,8 +35,8 @@ public IBinder onBind(Intent intent) {
3235
public int onStartCommand(Intent intent, int flags, int startId) {
3336
Intent notificationIntent = new Intent(this, LogcatActivity.class);
3437
int pendingIntentFlag;
35-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
36-
getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.S) {
38+
if (VERSION.SDK_INT >= VERSION_CODES.S &&
39+
getApplicationInfo().targetSdkVersion >= VERSION_CODES.S) {
3740
// Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
3841
// Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g.
3942
// if it needs to be used with inline replies or bubbles.
@@ -48,7 +51,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
4851
applicationName = getPackageName();
4952
}
5053

51-
Notification.Builder builder = new Notification.Builder(this)
54+
Builder builder = new Builder(this)
5255
// 设置大图标,不设置则默认为程序图标
5356
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logcat_floating_normal))
5457
// 设置标题
@@ -60,7 +63,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
6063
.setContentIntent(pendingIntent);
6164

6265
// 设置通知渠道
63-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
66+
if (VERSION.SDK_INT >= VERSION_CODES.O) {
6467
// 通知渠道的id
6568
String notificationChannelId = "logcat";
6669
NotificationChannel channel = new NotificationChannel(notificationChannelId,
@@ -79,15 +82,20 @@ public int onStartCommand(Intent intent, int flags, int startId) {
7982
builder.setChannelId(notificationChannelId);
8083
} else {
8184
// 关闭声音通知
82-
builder.setSound(null);
83-
// 关闭震动通知
84-
builder.setVibrate(null);
85-
// 关闭闪光灯通知
86-
builder.setLights(0, 0, 0);
85+
builder.setSound(null)
86+
// 关闭震动通知
87+
.setVibrate(null)
88+
// 关闭闪光灯通知
89+
.setLights(0, 0, 0);
8790
}
8891

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

0 commit comments

Comments
 (0)