|
| 1 | +/* -*- mode: java; c-basic-offset: 2; -*- */ |
| 2 | + |
| 3 | +package @SYS_PACKAGE_DOT@; |
| 4 | +@IF_ANDROIDAPI_GT_25@ |
| 5 | + import android.app.NotificationChannel; |
| 6 | +import android.app.NotificationManager; |
| 7 | +/* end of IF_ANDROIDAPI_GT_25 */ |
| 8 | + |
| 9 | +import android.util.Log; |
| 10 | +import android.app.Service; |
| 11 | +import android.app.Notification; |
| 12 | +import android.app.Notification.Builder; |
| 13 | + |
| 14 | +//import android.support.v4.app.NotificationCompat; |
| 15 | + |
| 16 | +import android.content.Intent; |
| 17 | +import android.os.IBinder; |
| 18 | +import android.os.SystemClock; |
| 19 | + |
| 20 | +public class LambdaNativeForegroundService extends Service { |
| 21 | + final static int notificationIsRunningId = 1; |
| 22 | + boolean running=true; |
| 23 | + Thread backgroundThread; |
| 24 | + public LambdaNativeForegroundService() { |
| 25 | + } |
| 26 | + private Notification.Builder make_notification_template() { |
| 27 | + return new Notification.Builder(this) |
| 28 | + .setContentTitle(getString(R.string.app_name)) |
| 29 | + // .setContentText("TBD") |
| 30 | + .setSmallIcon(R.drawable.icon) |
| 31 | + // .setLargeIcon(aBitmap) |
| 32 | + .setOngoing(true); |
| 33 | + } |
| 34 | + private void keepAwake_LT_API26() { |
| 35 | + startForeground(notificationIsRunningId, make_notification_template().build()); |
| 36 | + } |
| 37 | + private void keepAwake() { |
| 38 | + @IF_ANDROIDAPI_GT_25@ |
| 39 | + if(true) { |
| 40 | + NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
| 41 | + assert mgr != null; |
| 42 | + NotificationChannel channel = |
| 43 | + new NotificationChannel ("@SYS_PACKAGE_DOT@", ".working", NotificationManager.IMPORTANCE_NONE); |
| 44 | + mgr.createNotificationChannel(channel); |
| 45 | + Notification.Builder mknote = make_notification_template() |
| 46 | + .setChannelId("@SYS_PACKAGE_DOT@") |
| 47 | + .setCategory(Notification.CATEGORY_SERVICE); |
| 48 | + startForeground(notificationIsRunningId, mknote.build()); |
| 49 | + return; |
| 50 | + } |
| 51 | + /* end of IF_ANDROIDAPI_GT_25 */ |
| 52 | + keepAwake_LT_API26(); |
| 53 | + } |
| 54 | + @Override |
| 55 | + public IBinder onBind(Intent intent) { |
| 56 | + throw new UnsupportedOperationException("Not implemented"); |
| 57 | + } |
| 58 | + @Override |
| 59 | + public void onCreate() { |
| 60 | + // Log.d("","LambdaNativeForegroundService created"); |
| 61 | + super.onCreate(); |
| 62 | + keepAwake(); |
| 63 | + } |
| 64 | + @Override |
| 65 | + public void onStart(Intent intent, int startId) { |
| 66 | + // Log.d("","LambdaNativeForegroundService starting"); |
| 67 | + } |
| 68 | + @Override public int onStartCommand(Intent intent, int flags, int startId) { |
| 69 | + super.onStartCommand(intent, flags, startId); |
| 70 | + return START_STICKY; |
| 71 | + } |
| 72 | + @Override |
| 73 | + public void onDestroy() { |
| 74 | + // running=false; |
| 75 | + // Log.d("","LambdaNativeForegroundService stopped"); |
| 76 | + } |
| 77 | + // This is bound in the main class only!!! native void nativeEvent(int t, int x, int y); |
| 78 | +} |
0 commit comments