Skip to content

Commit

Permalink
优化 Android 14 前台 Service 服务类型新特性
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Aug 27, 2023
1 parent ad15e32 commit 01a3613
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 27 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.6/Logcat.apk)
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/Logcat/releases/download/11.8/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.6'
debugImplementation 'com.github.getActivity:Logcat:11.8'
}
```

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 app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.logcat.demo"
minSdk 16
targetSdk 34
versionCode 1160
versionName "11.6"
versionCode 1180
versionName "11.8"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ plugins {
id 'com.android.application' version '7.1.0' apply false
}

task clean(type: Delete) {
tasks.register('clean', Delete) {
delete rootProject.buildDir
}
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {

defaultConfig {
minSdkVersion 16
versionCode 1160
versionName "11.6"
versionCode 1180
versionName "11.8"
}

lintOptions {
Expand Down
13 changes: 4 additions & 9 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@
<!-- 前台 Service 权限 -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<!-- 前台 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" />
<!-- 前台 Service 权限用途声明 -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

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

</application>

Expand Down
30 changes: 19 additions & 11 deletions library/src/main/java/com/hjq/logcat/LogcatService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.hjq.logcat;

import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
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;
import android.text.TextUtils;

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

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

// 设置通知渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (VERSION.SDK_INT >= VERSION_CODES.O) {
// 通知渠道的id
String notificationChannelId = "logcat";
NotificationChannel channel = new NotificationChannel(notificationChannelId,
Expand All @@ -79,15 +82,20 @@ public int onStartCommand(Intent intent, int flags, int startId) {
builder.setChannelId(notificationChannelId);
} else {
// 关闭声音通知
builder.setSound(null);
// 关闭震动通知
builder.setVibrate(null);
// 关闭闪光灯通知
builder.setLights(0, 0, 0);
builder.setSound(null)
// 关闭震动通知
.setVibrate(null)
// 关闭闪光灯通知
.setLights(0, 0, 0);
}

// 将服务和通知绑定在一起,成为前台服务
startForeground(BACKUP_SERVICE_NOTIFICATION_ID, builder.build());
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());
}
return super.onStartCommand(intent, flags, startId);
}

Expand Down

0 comments on commit 01a3613

Please sign in to comment.