|
18 | 18 |
|
19 | 19 | import org.renpy.android.Hardware;
|
20 | 20 |
|
| 21 | +//new imports for channel definition |
| 22 | +import android.app.NotificationManager; |
| 23 | +import android.app.NotificationChannel; |
| 24 | +import android.content.Context; |
| 25 | +import android.graphics.Color; |
| 26 | +import android.os.Build; |
21 | 27 |
|
22 | 28 | public class PythonService extends Service implements Runnable {
|
23 | 29 |
|
@@ -116,7 +122,31 @@ protected void doStartForeground(Bundle extras) {
|
116 | 122 | builder.setSmallIcon(context.getApplicationInfo().icon);
|
117 | 123 | notification = builder.build();
|
118 | 124 | }
|
119 |
| - startForeground(1, notification); |
| 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 | + * this change is 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); |
120 | 150 | }
|
121 | 151 |
|
122 | 152 | @Override
|
|
0 commit comments