通过单击前景服务通知显示当前活动

问题描述 投票:0回答:1

如何通过单击通知显示前台服务活动?当我使用代码时,它将启动新的活动,但是我需要在服务正常运行的活动。这是我的代码(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;
        }
    }

并且在OnActivity的MainActivity中:

 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;

            }
}

希望您能帮助解决此问题。非常感谢。

java c# android xamarin android-8.0-oreo
1个回答
0
投票

我不清楚您要打开哪个活动

如何显示前台服务活动

前景服务独立于您的应用运行

您将在这里启动MainActivity:

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

您能在这里澄清您想做什么吗?

ps:我知道这不是答案,尚无法评论

© www.soinside.com 2019 - 2024. All rights reserved.