8000 doStartForeground() · rnixx/python-for-android@6ec1ac2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ec1ac2

Browse files
author
Philipp Auersperg-Castell
committed
doStartForeground()
1 parent 12a0f37 commit 6ec1ac2

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

pythonforandroid/bootstraps/service_library/build/src/main/java/org/kivy/android/PythonBoundService.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
package org.kivy.android;
22

3+
import android.app.Notification;
4+
import android.content.Intent;
5+
import android.app.PendingIntent;
6+
37
import android.app.Service;
48
import android.content.Context;
59
import android.os.Process;
610
import android.util.Log;
711

12+
//imports for channel definition
13+
import android.os.Build;
14+
import java.lang.reflect.Method;
15+
import java.lang.reflect.InvocationTargetException;
16+
17+
import android.app.NotificationManager;
18+
import android.app.NotificationChannel;
19+
import android.graphics.Color;
20+
821
import java.io.File;
922

1023
import org.kivy.android.PythonUtil;
@@ -29,6 +42,7 @@ public abstract class PythonBoundService extends Service implements Runnable {
2942

3043
// Argument to pass to Python code,
3144
private String pythonServiceArgument;
45+
public static PythonBoundService mService = null;
3246

3347
public void setPythonName(String value) {
3448
Log.d(TAG, "setPythonName()");
@@ -52,6 +66,7 @@ public void startPythonThread() {
5266
@Override
5367
public void onCreate() {
5468
super.onCreate();
69+
this.mService = this;
5570

5671
Log.d(TAG, "onCreate()");
5772

@@ -104,6 +119,8 @@ public void run() {
104119
pythonServiceArgument
105120
);
106121

122+
this.mService = this;
123+
107124
Log.d(TAG, "Python thread terminating");
108125
}
109126

@@ -114,4 +131,56 @@ public static native void nativeStart(
114131
String pythonHome, String pythonPath,
115132
String pythonServiceArgument
116133
);
134+
135+
/**
136+
* change the bound service to foreground service
137+
* automatically creates notification
138+
* @param serviceTitle: this text appears as notification title
139+
* @param serviceDescription: this text as notification description
140+
*/
141+
public void doStartForeground(String serviceTitle, String serviceDescription) {
142+
Notification notification;
143+
Context context = getApplicationContext();
144+
Intent contextIntent = new Intent(context, PythonActivity.class);
145+
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
146+
PendingIntent.FLAG_UPDATE_CURRENT);
147+
148+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
149+
notification = new Notification(
150+
context.getApplicationInfo().icon, serviceTitle, System.currentTimeMillis());
151+
try {
152+
// prevent using NotificationCompat, this saves 100kb on apk
153+
Method func = notification.getClass().getMethod(
154+
"setLatestEventInfo", Context.class, CharSequence.class,
155+
CharSequence.class, PendingIntent.class);
156+
func.invoke(notification, context, serviceTitle, serviceDescription, pIntent);
157+
} catch (NoSuchMethodException | IllegalAccessException |
158+
IllegalArgumentException | InvocationTargetException e) {
159+
}
160+
} else {
161+
// for android 8+ we need to create our own channel
162+
// https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1
163+
String NOTIFICATION_CHANNEL_ID = "org.kivy.p4a"; //TODO: make this configurable
164+
String channelName = "Background Service"; //TODO: make this configurable
165+
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName,
166+
NotificationManager.IMPORTANCE_NONE);
167+
168+
chan.setLightColor(Color.BLUE);
169+
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
170+
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
171+
manager.createNotificationChannel(chan);
172+
173+
Notification.Builder builder = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID);
174+
builder.setContentTitle(serviceTitle);
175+
builder.setContentText(serviceDescription);
176+
builder.setContentIntent(pIntent);
177+
builder.setSmallIcon(context.getApplicationInfo().icon);
178+
notification = builder.build();
179+
}
180+
startForeground(getServiceId(), notification);
181+
}
182+
183+
int getServiceId() {
184+
return 1;
185+
}
117186
}

0 commit comments

Comments
 (0)
0