Skip to content

Commit 1d375fb

Browse files
zworkbAndreMiras
authored andcommitted
service_only bootstrap now works with android (7 and 8 tested) (#1725)
service_only bootstrap now works with android (7 and 8 tested)
1 parent 6d038fa commit 1d375fb

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
import org.renpy.android.Hardware;
2020

21+
//imports for channel definition
22+
import android.app.NotificationManager;
23+
import android.app.NotificationChannel;
24+
import android.graphics.Color;
2125

2226
public class PythonService extends Service implements Runnable {
2327

@@ -90,13 +94,13 @@ public int onStartCommand(Intent intent, int flags, int startId) {
9094
protected void doStartForeground(Bundle extras) {
9195
String serviceTitle = extras.getString("serviceTitle");
9296
String serviceDescription = extras.getString("serviceDescription");
93-
9497
Notification notification;
9598
Context context = getApplicationContext();
9699
Intent contextIntent = new Intent(context, PythonActivity.class);
97100
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
98101
PendingIntent.FLAG_UPDATE_CURRENT);
99-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
102+
103+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
100104
notification = new Notification(
101105
context.getApplicationInfo().icon, serviceTitle, System.currentTimeMillis());
102106
try {
@@ -109,7 +113,19 @@ protected void doStartForeground(Bundle extras) {
109113
IllegalArgumentException | InvocationTargetException e) {
110114
}
111115
} else {
112-
Notification.Builder builder = new Notification.Builder(context);
116+
// for android 8+ we need to create our own channel
117+
// https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1
118+
String NOTIFICATION_CHANNEL_ID = "org.kivy.p4a"; //TODO: make this configurable
119+
String channelName = "PythonSerice"; //TODO: make this configurable
120+
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName,
121+
NotificationManager.IMPORTANCE_NONE);
122+
123+
chan.setLightColor(Color.BLUE);
124+
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
125+
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
126+
manager.createNotificationChannel(chan);
127+
128+
Notification.Builder builder = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID);
113129
builder.setContentTitle(serviceTitle);
114130
builder.setContentText(serviceDescription);
115131
builder.setContentIntent(pIntent);

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 %}

pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Service{{ name|capitalize }} extends PythonService {
1717
* {@inheritDoc}
1818
*/
1919
@Override
20-
public int getStartType() {
20+
public int startType() {
2121
return START_STICKY;
2222
}
2323
{% endif %}

pythonforandroid/recipes/android/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ def prebuild_arch(self, arch):
3838
bootstrap = bootstrap_name = ctx_bootstrap
3939

4040
is_sdl2 = bootstrap_name in ('sdl2', 'sdl2python3', 'sdl2_gradle')
41-
is_webview = bootstrap_name in ('webview',)
41+
is_webview = bootstrap_name == 'webview'
42+
is_service_only = bootstrap_name == 'service_only'
4243

43-
if is_sdl2 or is_webview:
44+
if is_sdl2 or is_webview or is_service_only:
4445
if is_sdl2:
4546
bootstrap = 'sdl2'
4647
java_ns = u'org.kivy.android'

testapps/setup_testapp_service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
'blacklist-requirements': 'openssl,sqlite3',
88
'android-api': 27,
99
'ndk-api': 21,
10-
'ndk-dir': '/home/asandy/android/crystax-ndk-10.3.2',
10+
'sdk-dir':'/opt/android/android-sdk/',
11+
'ndk-dir':'/opt/android/android-ndk-r17c/',
1112
'dist-name': 'testapp_service',
1213
'ndk-version': '10.3.2',
1314
'bootstrap': 'service_only',

0 commit comments

Comments
 (0)