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

Java 通过单击前台服务通知显示当前活动,java,c#,android,xamarin,android-8.0-oreo,Java,C#,Android,Xamarin,Android 8.0 Oreo,如何通过单击通知显示前台服务活动?当我使用我的代码时,它会启动新的活动,但我需要活动,服务在哪里工作。以下是我的代码(Android Oreo): 在OnCreate的主活动中: protected override void OnCreate(Bundle savedInstanceState) { if (!APSService.isRunning) { createNotificationCh

如何通过单击通知显示前台服务活动?当我使用我的代码时,它会启动新的活动,但我需要活动,服务在哪里工作。以下是我的代码(Android Oreo):

在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;

            }
}

我希望你能帮助解决这个问题。非常感谢。

我不清楚您想开展什么活动

如何显示前台服务活动

前台服务独立于应用程序运行

您正在此处启动Main活动:

Intent notificationIntent = new Intent(this,Java.Lang.Class.FromType((typeof(MainActivity))));
你能澄清一下你想在这里做什么吗


ps:我知道这不是一个答案,还不能评论

我写了一个演示,这是一个GIF

您可以实现如下代码所示的功能

 [Service]
class MyForegroundService : Service
{
    public const int SERVICE_RUNNING_NOTIFICATION_ID = 10000;

    [return: GeneratedEnum]
    public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
    {
        CreateNotificationChannel();
        string messageBody = "service starting";
       // / Create an Intent for the activity you want to start
       Intent resultIntent = new Intent(this,typeof(Activity1));
       // Create the TaskStackBuilder and add the intent, which inflates the back stack
       TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
       stackBuilder.AddNextIntentWithParentStack(resultIntent);
       // Get the PendingIntent containing the entire back stack
       PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, PendingIntentFlags.UpdateCurrent);
       var notification = new Notification.Builder(this, "10111")
        .SetContentIntent(resultPendingIntent)
        .SetContentTitle("Foreground")
        .SetContentText(messageBody)
        .SetSmallIcon(Resource.Drawable.main)
        .SetOngoing(true)
        .Build();
        StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification);
         //do you work
        return StartCommandResult.Sticky;


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

    void CreateNotificationChannel()
    {
        if (Build.VERSION.SdkInt < BuildVersionCodes.O)
        {
            // Notification channels are new in API 26 (and not a part of the
            // support library). There is no need to create a notification
            // channel on older versions of Android.
            return;
        }

        var channelName = Resources.GetString(Resource.String.channel_name);
        var channelDescription = GetString(Resource.String.channel_description);
        var channel = new NotificationChannel("10111", channelName, NotificationImportance.Default)
        {
            Description = channelDescription
        };

        var notificationManager = (NotificationManager)GetSystemService(NotificationService);
        notificationManager.CreateNotificationChannel(channel);
    }

}
[服务]
类MyForegroundService:服务
{
public const int SERVICE_RUNNING_NOTIFICATION_ID=10000;
[返回:GeneratedEnum]
公共覆盖StartCommandResult OnStartCommand(意向意向,[GenerateDum]StartCommandFlags标志,int-startId)
{
CreateNotificationChannel();
字符串messageBody=“服务启动”;
///为要启动的活动创建意图
意向结果=新意向(此,类型为(活动1));
//创建TaskStackBuilder并添加意图,这会使后堆栈膨胀
TaskStackBuilder stackBuilder=TaskStackBuilder.Create(此);
stackBuilder.AddNextEntWithParentStack(ResultEnt);
//获取包含整个后堆栈的Pending帐篷
PendingEvent ResultPendingEvent=stackBuilder.GetPendingEvent(0,PendingEventFlags.UpdateCurrent);
var notification=new notification.Builder(这是“10111”)
.SetContentIntent(结果结束内容)
.SetContentTitle(“前景”)
.SetContentText(messageBody)
.SetSmallIcon(Resource.Drawable.main)
.正在进行(正确)
.Build();
StartForeground(服务运行通知ID,通知);
//你工作吗
返回StartCommandResult.Sticky;
}
公共覆盖iBind OnBind(意图)
{
返回null;
}
void CreateNotificationChannel()
{
if(Build.VERSION.SdkInt
这是我的演示。

 [Service]
class MyForegroundService : Service
{
    public const int SERVICE_RUNNING_NOTIFICATION_ID = 10000;

    [return: GeneratedEnum]
    public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
    {
        CreateNotificationChannel();
        string messageBody = "service starting";
       // / Create an Intent for the activity you want to start
       Intent resultIntent = new Intent(this,typeof(Activity1));
       // Create the TaskStackBuilder and add the intent, which inflates the back stack
       TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
       stackBuilder.AddNextIntentWithParentStack(resultIntent);
       // Get the PendingIntent containing the entire back stack
       PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, PendingIntentFlags.UpdateCurrent);
       var notification = new Notification.Builder(this, "10111")
        .SetContentIntent(resultPendingIntent)
        .SetContentTitle("Foreground")
        .SetContentText(messageBody)
        .SetSmallIcon(Resource.Drawable.main)
        .SetOngoing(true)
        .Build();
        StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification);
         //do you work
        return StartCommandResult.Sticky;


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

    void CreateNotificationChannel()
    {
        if (Build.VERSION.SdkInt < BuildVersionCodes.O)
        {
            // Notification channels are new in API 26 (and not a part of the
            // support library). There is no need to create a notification
            // channel on older versions of Android.
            return;
        }

        var channelName = Resources.GetString(Resource.String.channel_name);
        var channelDescription = GetString(Resource.String.channel_description);
        var channel = new NotificationChannel("10111", channelName, NotificationImportance.Default)
        {
            Description = channelDescription
        };

        var notificationManager = (NotificationManager)GetSystemService(NotificationService);
        notificationManager.CreateNotificationChannel(channel);
    }

}