Показать текущую активность, щелкнув Уведомление службы Foreground - PullRequest
0 голосов
/ 21 ноября 2019

Как показать активность Foreground Service, нажав «Уведомление»? Когда я использую свой код, он начинает новое действие, но мне нужно действие, в котором работает служба. Вот мой код (Android Oreo):

 public class APSService : Service
    {
        public static bool isRunning = false;

        public override void OnCreate()
        {
            base.OnCreate();
        }

        public override void OnDestroy()
        {
            isRunning = false;
            base.OnDestroy();
        }



        [return: GeneratedEnum]
        public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            isRunning = true;
            byte[] input = intent.GetByteArrayExtra("inputExtra");

            Intent notificationIntent = new Intent(this, Java.Lang.Class.FromType((typeof(MainActivity))));

            PendingIntent pendingIntent = PendingIntent.GetActivity(this,
                0, notificationIntent, 0);

            var builder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
                .SetContentTitle("APS Service")
                .SetSmallIcon(Resource.Drawable.notifypump)
                .SetContentText("Start program...")
                .SetContentIntent(pendingIntent);

            Notification notification = builder.Build();

            StartForeground(1, notification);

            //do heavy work on background thread

            return StartCommandResult.NotSticky;
        }


        public override IBinder OnBind(Intent intent)
        {
            return null;
        }
    }

А в MainActivity в OnCreate:

 protected override void OnCreate(Bundle savedInstanceState)
        {
            if (!APSService.isRunning)
            {
                createNotificationChannel();
                startService();
            }
            else
            {
                NotificationChannel serviceChannel = new NotificationChannel
                       (
                           CHANNEL_ID,
                           "APS service Channel",
                           NotificationImportance.Default
                       );
                notificationManager = (NotificationManager)GetSystemService(Java.Lang.Class.FromType((typeof(NotificationManager))));
                notificationManager.CreateNotificationChannel(serviceChannel);
                UpdateNotification("Loading...");
                APSService.isRunning = true;

            }
}

Надеюсь, вы поможете решить эту проблему. Большое спасибо.

...