如何在 MAUI Android 中停止前台服务?

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

我正在尝试创建一个服务,当我看到一个开关时我可以启动和停止。

这是我的代码:

[Service]
public class MyForegroundService : Service
{
    private string NOTIFICATION_CHANNEL_ID = "1000";
    private int NOTIFICATION_ID = 1;
    private string NOTIFICATION_CHANNEL_NAME = "notification";
    private NotificationManager _notificationManager;





    public MyForegroundService()
    {
    }



    private void StartForegroundService()
    {
        _notificationManager = (GetSystemService(Context.NotificationService) as NotificationManager)!;

        createNotificationChannel(_notificationManager);

        Notification notification = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
            .SetAutoCancel(false)
            .SetOngoing(true)
            .Build();
        StartForeground(NOTIFICATION_ID, notification);
    }


    private NotificationChannel? createNotificationChannel(NotificationManager notificationMnaManager)
    {
        if (Build.VERSION.SdkInt < BuildVersionCodes.O) return null;

        NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationImportance.Default);
        notificationMnaManager.CreateNotificationChannel(channel);

        return channel;
    }

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


    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        if(intent.Action == "START_SERVICE")
        {
            StartForegroundService();
        }
        else if(intent.Action == "STOP_SERVICE")
        {
            StopForeground(true);
            StopSelfResult(startId);
        }



        return StartCommandResult.NotSticky;
    }





    public void Start()
    {
        Intent intent = new Intent(Android.App.Application.Context, typeof(MyForegroundService));
        intent.SetAction("START_SERVICE");
        Android.App.Application.Context.StartForegroundService(intent);
    }



    public void STOP()
    {
        Intent intent = new Intent(Android.App.Application.Context, typeof(MyForegroundService));
        intent.SetAction("STOP_SERVICE");
        Android.App.Application.Context.StartForegroundService(intent);
    }
}

我已经意识到,如果我在 OnStartCommand() 中执行此操作,我可以启动和停止前台服务。如果我尝试在外面做,那是行不通的。

为此,我的Stop()方法的实现是这样的:

public void STOP()
{
    Intent intent = new Intent(Android.App.Application.Context, typeof(MyForegroundService));
    intent.SetAction("STOP_SERVICE");
    Android.App.Application.Context.StartForegroundService(intent);
}

此代码的问题是我正在使用 StartForegroundService() 强制执行 OnStartCommand() 中的代码。但这看起来是一个糟糕的解决方法,因为 StartForegroundService 应该用于启动前台,而不是停止它。

但是,我尝试了 Stop() 方法的其他实现,但没有一个有效。像这样的东西:

public void STOP()
{
    var intent = new Intent(Android.App.Application.Context, this.Class);
    intent.SetAction("STOP_SERVICE");
    Android.App.Application.Context.StopService(intent);
}

我尝试了一个示例应用程序,我在 youtube 上发现它可以工作。实现是这样的:

public void Stop()
{
    Intent stopIntent = new Intent(MainActivity.ActivityCurrent, this.Class);
    stopIntent.SetAction("STOP_SERVICE");
    MainActivity.ActivityCurrent.StartService(stopIntent);
}

但在我的例子中,MainActivity.ActivityCurrent 不可用。我也不是以这种方式创建意图的原因。为什么会用到Android的MainActivity?改用应用程序上下文不是更好吗?有什么区别?

总而言之,我想知道我应该如何停止服务。

非常感谢。

android maui foreground-service
1个回答
0
投票

我创建了一个新项目来测试在毛伊岛的 ContentPage 按钮单击事件中启动和停止前台服务。您可以使用

Android.App.Application.Context.StopService()
停止前台服务。

启动前台服务:

private void StartButton_Clicked(object sender, EventArgs e)
{
#if ANDROID
    Android.Content.Intent intent = new Android.Content.Intent(Android.App.Application.Context,typeof(ForegroundServiceDemo));
    Android.App.Application.Context.StartForegroundService(intent);
#endif
    }

停止按钮:

    private void StopButton_Clicked(object sender, EventArgs e)
    {
#if ANDROID
        Android.Content.Intent intent = new Android.Content.Intent(Android.App.Application.Context, typeof(ForegroundServiceDemo));
        Android.App.Application.Context.StopService(intent);
#endif
    }
© www.soinside.com 2019 - 2024. All rights reserved.