示例#1
0
        private void startMyService()
        {
            //create the default activity to be shown
            Intent        intent  = new Intent();
            PendingIntent pending = PendingIntent.GetActivity(this, 0, intent, 0);


            if (Android.OS.Build.VERSION.SdkInt <= BuildVersionCodes.NMr1)
            {
                var notification = new Notification.Builder(this, Constants.CHANNEL_ID)
                                   .SetContentTitle(Constants.CONTENT_TITLE)
                                   .SetContentText(Constants.CONTENT_TEXT)
                                   .SetSmallIcon(Resource.Drawable.bsplash)
                                   .SetOngoing(true)
                                   .Build();

                StartForeground(300, notification);
            }

            else
            {
                //create a notification channel. this is a must if your service is going to run on
                //Android 8.0(Oreo) and above
                NotificationChannel channel = new NotificationChannel(MPConstants.CHANNEL_ID1, MPConstants.CHANNEL_NAME, NotificationImportance.Default);
                NotificationManager manager = GetSystemService(NotificationService) as NotificationManager;

                if (manager != null)
                {
                    manager.CreateNotificationChannel(channel);
                }

                //create the notification
                var builder = new Notification.Builder(this, MPConstants.CHANNEL_ID1);

                //make notification show big text
                Notification.BigTextStyle BigStyle = new Notification.BigTextStyle();
                BigStyle.SetBigContentTitle("This service is running in the foreground");
                BigStyle.BigText("This is a music like foreground service that was built using some tutorial and some little tweaks");
                builder.SetStyle(BigStyle);

                //build notification
                builder.SetWhen(DateTime.Now.Millisecond);
                builder.SetSmallIcon(Resource.Drawable.badge);
                //////set large icon of type Bitmap
                //Bitmap LargeIconBitMap = BitmapFactory.DecodeResource(Resources.get, Resource.Drawable.bsplash);

                //used to set full screen intent
                builder.SetFullScreenIntent(pending, true);

                //Add play button to Notification
                Intent PlayIntent = new Intent(this, typeof(MusicService));
                intent.SetAction(MPConstants.ACTION_PLAY);
                PendingIntent       PendingPlayIntent = PendingIntent.GetService(this, 0, PlayIntent, 0);
                Notification.Action playaction        = new Notification.Action(Resource.Drawable.ic_media_play_light, "Play", PendingPlayIntent);
                builder.AddAction(playaction);

                //add Pause button to Notification
                Intent PauseIntent = new Intent(this, typeof(MusicService));
                intent.SetAction(MPConstants.ACTION_PAUSE);
                PendingIntent       PausePendingIntent = PendingIntent.GetService(this, 0, PauseIntent, 0);
                Notification.Action PauseAction        = new Notification.Action(Resource.Drawable.ic_media_pause_light, "Pause", PausePendingIntent);
                builder.AddAction(PauseAction);

                //build notification
                Notification notification = builder.Build();

                StartForeground(500, notification);
            }
        }