19
19
import android .app .Service ;
20
20
import android .content .Intent ;
21
21
import android .content .SharedPreferences ;
22
+ import android .content .pm .ServiceInfo ;
22
23
import android .content .res .Resources ;
23
24
import android .os .Binder ;
24
25
import android .os .Build ;
@@ -232,17 +233,15 @@ public void onDestroy() {
232
233
}
233
234
234
235
private Notification makeNotification (Intent intent , int icon , String title , String description ) {
235
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
236
- NotificationManager notificationManager =
237
- (NotificationManager ) getSystemService (NOTIFICATION_SERVICE );
238
- NotificationChannel channel =
239
- new NotificationChannel (TAG , TAG , NotificationManager .IMPORTANCE_LOW );
240
- if (notificationManager != null ) {
241
- notificationManager .createNotificationChannel (channel );
242
- }
236
+ NotificationManager notificationManager =
237
+ (NotificationManager ) getSystemService (NOTIFICATION_SERVICE );
238
+ NotificationChannel channel =
239
+ new NotificationChannel (TAG , TAG , NotificationManager .IMPORTANCE_LOW );
240
+ if (notificationManager != null ) {
241
+ notificationManager .createNotificationChannel (channel );
243
242
}
244
243
245
- PendingIntent pi = PendingIntent .getActivity (getApplicationContext (), 0 , intent , 0 );
244
+ PendingIntent pi = PendingIntent .getActivity (getApplicationContext (), 0 , intent , PendingIntent . FLAG_IMMUTABLE );
246
245
return new NotificationCompat .Builder (PdService .this , TAG )
247
246
.setSmallIcon (icon )
248
247
.setContentTitle (title )
@@ -256,7 +255,15 @@ private Notification makeNotification(Intent intent, int icon, String title, Str
256
255
257
256
private void startForeground (Notification notification ) {
258
257
stopForeground ();
259
- startForeground (NOTIFICATION_ID , notification );
258
+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .Q ) {
259
+ int foregroundServiceTypes = ServiceInfo .FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK ;
260
+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .R ) {
261
+ foregroundServiceTypes |= ServiceInfo .FOREGROUND_SERVICE_TYPE_MICROPHONE ;
262
+ }
263
+ startForeground (NOTIFICATION_ID , notification , foregroundServiceTypes );
264
+ } else {
265
+ startForeground (NOTIFICATION_ID , notification );
266
+ }
260
267
hasForeground = true ;
261
268
}
262
269
0 commit comments