31
31
32
32
import android .annotation .SuppressLint ;
33
33
import android .app .Notification ;
34
+ import android .app .NotificationChannel ;
35
+ import android .app .NotificationManager ;
34
36
import android .app .PendingIntent ;
35
37
import android .app .Service ;
36
38
import android .content .BroadcastReceiver ;
@@ -65,6 +67,8 @@ public class UsbIpService extends Service implements UsbRequestHandler {
65
67
private static final boolean DEBUG = false ;
66
68
67
69
private static final int NOTIFICATION_ID = 100 ;
70
+
71
+ private final static String CHANNEL_ID = "serviceInfo" ;
68
72
69
73
private static final String ACTION_USB_PERMISSION =
70
74
"org.cgutman.usbip.USB_PERMISSION" ;
@@ -86,16 +90,29 @@ public void onReceive(Context context, Intent intent) {
86
90
private void updateNotification () {
87
91
Intent intent = new Intent (this , UsbIpConfig .class );
88
92
intent .setFlags (Intent .FLAG_ACTIVITY_CLEAR_TOP | Intent .FLAG_ACTIVITY_SINGLE_TOP );
89
- PendingIntent pendIntent = PendingIntent .getActivity (this , 0 , intent , 0 );
90
-
91
- Notification .Builder builder = new Notification .Builder (this );
92
- builder
93
- .setTicker ("USB/IP Server Running" )
94
- .setContentTitle ("USB/IP Server Running" )
95
- .setAutoCancel (false )
96
- .setOngoing (true )
97
- .setSmallIcon (R .drawable .notification_icon )
98
- .setContentIntent (pendIntent );
93
+
94
+ int intentFlags = 0 ;
95
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
96
+ intentFlags |= PendingIntent .FLAG_IMMUTABLE ;
97
+ }
98
+
99
+ PendingIntent pendIntent = PendingIntent .getActivity (this , 0 , intent , intentFlags );
100
+
101
+ Notification .Builder builder = new Notification .Builder (this )
102
+ .setTicker ("USB/IP Server Running" )
103
+ .setContentTitle ("USB/IP Server Running" )
104
+ .setAutoCancel (false )
105
+ .setOngoing (true )
106
+ .setSmallIcon (R .drawable .notification_icon )
107
+ .setContentIntent (pendIntent );
108
+
109
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
110
+ builder .setChannelId (CHANNEL_ID );
111
+ }
112
+
113
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
114
+ builder .setForegroundServiceBehavior (Notification .FOREGROUND_SERVICE_IMMEDIATE );
115
+ }
99
116
100
117
if (connections .size () == 0 ) {
101
118
builder .setContentText ("No devices currently shared" );
@@ -146,6 +163,12 @@ public void onCreate() {
146
163
147
164
server = new UsbIpServer ();
148
165
server .start (this );
166
+
167
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
168
+ NotificationChannel channel = new NotificationChannel (CHANNEL_ID , "Service Info" , NotificationManager .IMPORTANCE_DEFAULT );
169
+ NotificationManager notificationManager = getSystemService (NotificationManager .class );
170
+ notificationManager .createNotificationChannel (channel );
171
+ }
149
172
150
173
updateNotification ();
151
174
}
0 commit comments