11package com .hjq .logcat ;
22
3- import android .app .Notification ;
3+ import android .app .Notification . Builder ;
44import android .app .NotificationChannel ;
55import android .app .NotificationManager ;
66import android .app .PendingIntent ;
77import android .app .Service ;
88import android .content .Context ;
99import android .content .Intent ;
1010import android .content .pm .PackageManager ;
11+ import android .content .pm .ServiceInfo ;
1112import android .graphics .BitmapFactory ;
1213import android .os .Build ;
14+ import android .os .Build .VERSION ;
15+ import android .os .Build .VERSION_CODES ;
1316import android .os .IBinder ;
1417import 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