Skip to content

Commit 9ffb2d4

Browse files
committed
create new channel (needed for android 8+)
1 parent 8170aeb commit 9ffb2d4

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
import org.renpy.android.Hardware;
2020

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;
2127

2228
public class PythonService extends Service implements Runnable {
2329

@@ -116,7 +122,31 @@ protected void doStartForeground(Bundle extras) {
116122
builder.setSmallIcon(context.getApplicationInfo().icon);
117123
notification = builder.build();
118124
}
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);
120150
}
121151

122152
@Override

pythonforandroid/bootstraps/service_only/build/templates/AndroidManifest.tmpl.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@
7171

7272
{% if service %}
7373
<service android:name="org.kivy.android.PythonService"
74-
android:process=":pythonservice" />
74+
android:process=":pythonservice"
75+
android:exported="true"/>
7576
{% endif %}
7677
{% for name in service_names %}
7778
<service android:name="{{ args.package }}.Service{{ name|capitalize }}"
78-
android:process=":service_{{ name }}" />
79+
android:process=":service_{{ name }}"
80+
android:exported="true" />
7981
{% endfor %}
8082

8183
{% if args.billing_pubkey %}

0 commit comments

Comments
 (0)