|
1 | 1 | package org.kivy.android; |
2 | 2 |
|
| 3 | +import android.app.Activity; |
| 4 | +import android.app.PendingIntent; |
3 | 5 | import android.app.Service; |
| 6 | +import android.content.Context; |
4 | 7 | import android.content.Intent; |
5 | 8 | import android.os.Bundle; |
6 | 9 | import android.os.IBinder; |
7 | 10 | import android.os.Process; |
| 11 | +import android.support.v4.app.NotificationCompat; |
8 | 12 | import android.util.Log; |
9 | 13 |
|
10 | 14 | public class PythonService extends Service implements Runnable { |
@@ -33,6 +37,14 @@ public void setAutoRestartService(boolean restart) { |
33 | 37 | autoRestartService = restart; |
34 | 38 | } |
35 | 39 |
|
| 40 | + public boolean canDisplayNotification() { |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + public int startType() { |
| 45 | + return START_NOT_STICKY; |
| 46 | + } |
| 47 | + |
36 | 48 | /** |
37 | 49 | * {@inheritDoc} |
38 | 50 | */ |
@@ -76,30 +88,35 @@ public int onStartCommand(Intent intent, int flags, int startId) { |
76 | 88 | pythonThread = new Thread(this); |
77 | 89 | pythonThread.start(); |
78 | 90 |
|
79 | | - if (canDisplayNotification()) { |
80 | | - doStartForeground(extras); |
81 | | - } |
| 91 | + if (canDisplayNotification()) { |
| 92 | + doStartForeground(extras); |
| 93 | + } |
82 | 94 |
|
83 | 95 | return startType(); |
84 | 96 | } |
85 | 97 |
|
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 | + /** |
103 | 120 | * {@inheritDoc} |
104 | 121 | */ |
105 | 122 | @Override |
|
0 commit comments