|
1 | 1 | package org.kivy.android; |
2 | 2 |
|
| 3 | +import android.os.Build; |
| 4 | +import java.lang.reflect.Method; |
| 5 | +import java.lang.reflect.InvocationTargetException; |
3 | 6 | import android.app.Service; |
4 | 7 | import android.os.IBinder; |
5 | 8 | import android.os.Bundle; |
@@ -88,13 +91,31 @@ protected void doStartForeground(Bundle extras) { |
88 | 91 | String serviceTitle = extras.getString("serviceTitle"); |
89 | 92 | String serviceDescription = extras.getString("serviceDescription"); |
90 | 93 |
|
| 94 | + Notification notification; |
91 | 95 | Context context = getApplicationContext(); |
92 | | - Notification notification = new Notification(context.getApplicationInfo().icon, |
93 | | - serviceTitle, System.currentTimeMillis()); |
94 | 96 | Intent contextIntent = new Intent(context, PythonActivity.class); |
95 | 97 | PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent, |
96 | 98 | PendingIntent.FLAG_UPDATE_CURRENT); |
97 | | - notification.setLatestEventInfo(context, serviceTitle, serviceDescription, pIntent); |
| 99 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { |
| 100 | + notification = new Notification( |
| 101 | + context.getApplicationInfo().icon, serviceTitle, System.currentTimeMillis()); |
| 102 | + try { |
| 103 | + // prevent using NotificationCompat, this saves 100kb on apk |
| 104 | + Method func = notification.getClass().getMethod( |
| 105 | + "setLatestEventInfo", Context.class, CharSequence.class, |
| 106 | + CharSequence.class, PendingIntent.class); |
| 107 | + func.invoke(notification, context, serviceTitle, serviceDescription, pIntent); |
| 108 | + } catch (NoSuchMethodException | IllegalAccessException | |
| 109 | + IllegalArgumentException | InvocationTargetException e) { |
| 110 | + } |
| 111 | + } else { |
| 112 | + Notification.Builder builder = new Notification.Builder(context); |
| 113 | + builder.setContentTitle(serviceTitle); |
| 114 | + builder.setContentText(serviceDescription); |
| 115 | + builder.setContentIntent(pIntent); |
| 116 | + builder.setSmallIcon(context.getApplicationInfo().icon); |
| 117 | + notification = builder.build(); |
| 118 | + } |
98 | 119 | startForeground(1, notification); |
99 | 120 | } |
100 | 121 |
|
|
0 commit comments