diff --git a/android/AndroidManifest.xml b/android/AndroidManifest.xml index 6de3d89eb..01ff051c4 100644 --- a/android/AndroidManifest.xml +++ b/android/AndroidManifest.xml @@ -11,6 +11,7 @@ + ncClass = Class.forName("android.app.NotificationChannel"); + Constructor ncCtor = ncClass.getConstructor(String.class, CharSequence.class, int.class); + Object nc = ncCtor.newInstance(id, name, importance); + + Method nmCreateNotificationChannelMethod = + NotificationManager.class.getMethod("createNotificationChannel", ncClass); + nmCreateNotificationChannelMethod.invoke(notificationManager, nc); + + Constructor nbCtor = Notification.Builder.class.getConstructor(Context.class, String.class); + return (Notification.Builder) nbCtor.newInstance(this, id); + } catch (Exception e) + { + Log.e(TAG, "error creating the NotificationChannel", e); + return null; + } + } + private void start() { if (mThread != null) return; - mThread = new Thread(this); - mThread.start(); final Intent mainIntent = new Intent(this, Settings.class); mainIntent.setAction("android.intent.action.MAIN"); @@ -168,13 +197,25 @@ public class Main extends Service implements Runnable { final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_CANCEL_CURRENT); - Notification notification = new Notification.Builder(this) - .setContentTitle(getText(R.string.notification_title_mpd_running)) + Notification.Builder nBuilder; + if (Build.VERSION.SDK_INT >= 26 /* Build.VERSION_CODES.O */) + { + nBuilder = createNotificationBuilderWithChannel(); + if (nBuilder == null) + return; + } + else + nBuilder = new Notification.Builder(this); + + Notification notification = nBuilder.setContentTitle(getText(R.string.notification_title_mpd_running)) .setContentText(getText(R.string.notification_text_mpd_running)) .setSmallIcon(R.drawable.notification_icon) .setContentIntent(contentIntent) .build(); + mThread = new Thread(this); + mThread.start(); + startForeground(R.string.notification_title_mpd_running, notification); startService(new Intent(this, Main.class)); }