我想检查前台服务是否已经在 Android 的 MAUI 中运行,那么我不必执行任何操作,但如果没有运行,我会自动启动它。我尝试了 Intent.Action,但它总是显示错误,即使前台服务正在运行,因为它显示定期警报。
您可以使用 Android.OS.Binder 来实现此目的,这将使您能够与您的服务进行通信。下面,我创建了一个简化的示例,如何实现这一点:
实现 OnBind 和 OnUnbind 的自定义服务:
[Service]
public class MyService : Service
{
private const string _channelId = "123";
private const int _notifID = 333;
public bool IsServiceRunning { get; set; }
public override IBinder OnBind(Intent intent)
{
IBinder _binder = new ServiceBinder(this);
StartService();
return _binder;
}
public override bool OnUnbind(Intent intent)
{
IsServiceRunning = false;
return base.OnUnbind(intent);
}
public override void OnDestroy()
{
base.OnDestroy();
}
public void StartService()
{
Notification notification = CreateNotification();
StartForeground(_notifID, notification);
IsServiceRunning = true;
}
public Notification CreateNotification()
{
Context context = Platform.AppContext;
var intent = new Intent(context, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
var pendingIntent = PendingIntent.GetActivity(context, 111, intent, PendingIntentFlags.Immutable);
var notificationBuilder = new NotificationCompat.Builder(context, _channelId)
.SetContentTitle("ForegroundService")
.SetContentText("Message")
.SetSmallIcon(Resource.Drawable.dotnet_bot)
.SetOngoing(true)
.SetContentIntent(pendingIntent);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
NotificationChannel notificationChannel = new NotificationChannel(_channelId, "Title", NotificationImportance.High);
notificationChannel.SetShowBadge(true);
var notificationManager = context.GetSystemService(Context.NotificationService) as NotificationManager;
if (notificationManager != null)
{
notificationBuilder.SetChannelId(_channelId);
notificationManager.CreateNotificationChannel(notificationChannel);
}
}
return notificationBuilder.Build();
}
}
服务连接:
public class MyServiceConnection : Java.Lang.Object, IServiceConnection
{
public ServiceBinder Binder { get; set; }
public void OnServiceConnected(ComponentName name, IBinder service)
{
Binder = (ServiceBinder)service;
}
public void OnServiceDisconnected(ComponentName name)
{
}
public MyService GetService()
{
if (Binder == null)
return null;
return Binder.GetService();
}
}
public class ServiceBinder : Binder
{
private MyService _service;
public ServiceBinder(MyService service)
{
_service = service;
}
public MyService GetService()
{
return _service;
}
}
ForgroundService 用于启动服务并检查其是否正在运行:
public class ForegroundService
{
private readonly MyServiceConnection _serviceConnection;
public ForegroundService(MyServiceConnection serviceConnection)
{
_serviceConnection = serviceConnection;
}
public void StartForegroundService()
{
Context context = Platform.AppContext;
Intent intent = new(context, typeof(MyService));
context.BindService(intent, _serviceConnection, Bind.AutoCreate);
}
public bool IsForegroundServiceRunning()
{
if (_serviceConnection.GetService() == null || !_serviceConnection.GetService().IsServiceRunning)
return false;
return true;
}
}
服务可以在代码中运行和检查:
MyServiceConnection msc = new MyServiceConnection();
ForegroundService fs = new ForegroundService(msc);
fs.IsForegroundServiceRunning();