Skip to content

Commit 6b30ca3

Browse files
committed
bugfix foreground service
1 parent bfc0bb3 commit 6b30ca3

1 file changed

Lines changed: 37 additions & 20 deletions

File tree

pythonforandroid/bootstraps/service_only/build/src/org/kivy/android/PythonService.java

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

3+
import android.app.Activity;
4+
import android.app.PendingIntent;
35
import android.app.Service;
6+
import android.content.Context;
47
import android.content.Intent;
58
import android.os.Bundle;
69
import android.os.IBinder;
710
import android.os.Process;
11+
import android.support.v4.app.NotificationCompat;
812
import android.util.Log;
913

1014
public class PythonService extends Service implements Runnable {
@@ -33,6 +37,14 @@ public void setAutoRestartService(boolean restart) {
3337
autoRestartService = restart;
3438
}
3539

40+
public boolean canDisplayNotification() {
41+
return true;
42+
}
43+
44+
public int startType() {
45+
return START_NOT_STICKY;
46+
}
47+
3648
/**
3749
* {@inheritDoc}
3850
*/
@@ -76,30 +88,35 @@ public int onStartCommand(Intent intent, int flags, int startId) {
7688
pythonThread = new Thread(this);
7789
pythonThread.start();
7890

79-
if (canDisplayNotification()) {
80-
doStartForeground(extras);
81-
}
91+
if (canDisplayNotification()) {
92+
doStartForeground(extras);
93+
}
8294

8395
return startType();
8496
}
8597

86-
protected void doStartForeground(Bundle extras) {
87-
String serviceTitle = extras.getString("serviceTitle");
88-
String serviceDescription = extras.getString("serviceDescription");
89-
90-
Context context = getApplicationContext();
91-
Notification notification = new Notification(
92-
context.getApplicationInfo().icon, serviceTitle,
93-
System.currentTimeMillis());
94-
Intent contextIntent = new Intent(context, PythonActivity.class);
95-
PendingIntent pIntent = PendingIntent.getActivity(context, 0,
96-
contextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
97-
notification.setLatestEventInfo(context, serviceTitle,
98-
serviceDescription, pIntent);
99-
startForeground(1, notification);
100-
}
101-
102-
/**
98+
protected void doStartForeground(Bundle extras) {
99+
String serviceTitle = extras.getString("serviceTitle");
100+
String serviceDescription = extras.getString("serviceDescription");
101+
102+
Context context = getApplicationContext();
103+
104+
NotificationCompat.Builder builder =
105+
new NotificationCompat.Builder(this)
106+
.setSmallIcon(context.getApplicationInfo().icon)
107+
.setContentTitle(serviceTitle)
108+
.setContentText(serviceDescription);
109+
110+
int NOTIFICATION_ID = 1;
111+
112+
Intent targetIntent = new Intent(this, Activity.class);
113+
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
114+
builder.setContentIntent(contentIntent);
115+
116+
startForeground(NOTIFICATION_ID, builder.build());
117+
}
118+
119+
/**
103120
* {@inheritDoc}
104121
*/
105122
@Override

0 commit comments

Comments
 (0)