@@ -96,13 +96,13 @@ public int onStartCommand(Intent intent, int flags, int startId) {
96
96
protected void doStartForeground (Bundle extras ) {
97
97
String serviceTitle = extras .getString ("serviceTitle" );
98
98
String serviceDescription = extras .getString ("serviceDescription" );
99
-
100
99
Notification notification ;
101
100
Context context = getApplicationContext ();
102
101
Intent contextIntent = new Intent (context , PythonActivity .class );
103
102
PendingIntent pIntent = PendingIntent .getActivity (context , 0 , contextIntent ,
104
103
PendingIntent .FLAG_UPDATE_CURRENT );
105
- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .HONEYCOMB ) {
104
+
105
+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O ) {
106
106
notification = new Notification (
107
107
context .getApplicationInfo ().icon , serviceTitle , System .currentTimeMillis ());
108
108
try {
@@ -115,38 +115,27 @@ protected void doStartForeground(Bundle extras) {
115
115
IllegalArgumentException | InvocationTargetException e ) {
116
116
}
117
117
} else {
118
- Notification .Builder builder = new Notification .Builder (context );
118
+ // for android 8+ we need to create our own channel
119
+ // https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1
120
+ String NOTIFICATION_CHANNEL_ID = "org.kivy.p4a" ; //TODO: make this configurable
121
+ String channelName = "PythonSerice" ; //TODO: make this configurable
122
+ NotificationChannel chan = new NotificationChannel (NOTIFICATION_CHANNEL_ID , channelName ,
123
+ NotificationManager .IMPORTANCE_NONE );
124
+
125
+ chan .setLightColor (Color .BLUE );
126
+ chan .setLockscreenVisibility (Notification .VISIBILITY_PRIVATE );
127
+ NotificationManager manager = (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
128
+ assert manager != null ;
129
+ manager .createNotificationChannel (chan );
130
+
131
+ Notification .Builder builder = new Notification .Builder (context , NOTIFICATION_CHANNEL_ID );
119
132
builder .setContentTitle (serviceTitle );
120
133
builder .setContentText (serviceDescription );
121
134
builder .setContentIntent (pIntent );
122
135
builder .setSmallIcon (context .getApplicationInfo ().icon );
123
136
notification = builder .build ();
124
137
}
125
-
126
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ){
127
- Log .v ("python service" ,"android 8: start foregrund with new channel" );
128
- startForegroundWithNewChannel (1 , notification );
129
- }else {
130
- Log .v ("python service" ,"android <8: start foregrund withut new channel" );
131
- startForeground (1 , notification );
132
- }
133
- }
134
-
135
- /**
136
- * https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1
137
- *
138
- * necessary because from Android 8 on we need our own channel
139
- */
140
- private void startForegroundWithNewChannel (int id , Notification notification ){
141
- String NOTIFICATION_CHANNEL_ID = "org.bd.pureservice" ; //TODO: dynamically
142
- String channelName = "Pureservice" ;
143
- NotificationChannel chan = new NotificationChannel (NOTIFICATION_CHANNEL_ID , channelName , NotificationManager .IMPORTANCE_NONE );
144
- chan .setLightColor (Color .BLUE );
145
- chan .setLockscreenVisibility (Notification .VISIBILITY_PRIVATE );
146
- NotificationManager manager = (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
147
- assert manager != null ;
148
- manager .createNotificationChannel (chan );
149
- startForeground (id , notification );
138
+ startForeground (1 , notification );
150
139
}
151
140
152
141
@ Override
0 commit comments