Skip to content

Commit d48670b

Browse files
committed
startForegrund now works on Android 8
1 parent 9bce9b5 commit d48670b

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonService.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ public int onStartCommand(Intent intent, int flags, int startId) {
9696
protected void doStartForeground(Bundle extras) {
9797
String serviceTitle = extras.getString("serviceTitle");
9898
String serviceDescription = extras.getString("serviceDescription");
99-
10099
Notification notification;
101100
Context context = getApplicationContext();
102101
Intent contextIntent = new Intent(context, PythonActivity.class);
103102
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
104103
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) {
106106
notification = new Notification(
107107
context.getApplicationInfo().icon, serviceTitle, System.currentTimeMillis());
108108
try {
@@ -115,38 +115,27 @@ protected void doStartForeground(Bundle extras) {
115115
IllegalArgumentException | InvocationTargetException e) {
116116
}
117117
} 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);
119132
builder.setContentTitle(serviceTitle);
120133
builder.setContentText(serviceDescription);
121134
builder.setContentIntent(pIntent);
122135
builder.setSmallIcon(context.getApplicationInfo().icon);
123136
notification = builder.build();
124137
}
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);
150139
}
151140

152141
@Override

0 commit comments

Comments
 (0)