Hello,
Welcome to our Microsoft Q&A platform!
I am not familar with SignalR
. Do you want to achieve a android service that execute your SignalR
code in Xamarin?
If Yes, you need to know the Background Execution Limits in Android 8.0. SO, I advice you to use Foreground service to achieve it.
And I notice you use Xamarin.Forms, if you want to start a service, create an Interface in the Xamarin.forms project:
public interface IStartService
{
void StartForegroundServiceCompat();
}
if you execute your dependence service, you can use following code.
DependencyService.Get<IStartService>().StartForegroundServiceCompat();
Then create a startServiceAndroid.cs
in xxx.Android project to start the a service (Before android 8.0, start a service After Android 8.0 or later, you need to open a foreground service). Please see the //do you work
, you can edd the new BackgroundStartup();
. When the android background service is running, your BackgroundStartup
could run it.
[assembly: Dependency(typeof(startServiceAndroid))]
namespace ShinyExample.Droid
{
[Service]
class startServiceAndroid: Service, IStartService
{
public void StartForegroundServiceCompat()
{
var intent = new Intent(Android.App.Application.Context, typeof(startServiceAndroid));
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
Android.App.Application.Context.StartForegroundService(intent);
}
else
{
Android.App.Application.Context.StartService(intent);
}
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public const int SERVICE_RUNNING_NOTIFICATION_ID = 10000;
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
// From shared code or in your PCL
CreateNotificationChannel();
string messageBody = "service starting";
var notification = new Notification.Builder(this, "10111")
.SetContentTitle("Foreground")
.SetContentText(messageBody)
.SetSmallIcon(Resource.Drawable.icon1)
.SetOngoing(true)
.Build();
StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification);
//do you work
new BackgroundStartup();
return StartCommandResult.Sticky;
}
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.channelName);
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);
}
}
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.