我想向Xamarin.Forms应用程序添加后台服务,以每隔X分钟从服务器获取一些数据..因此,我添加了BroadcastReceiver,服务和警报管理器来完成这项工作,并且该服务成功启动单击按钮,但是当我尝试停止该服务时,它不会停止!
此类可打开/关闭服务。
[assembly: Dependency(typeof(Notifications))]
namespace FleetDesigner.Droid
{
class Notifications : Activity, IAndroidNotifications
{
Context context = Android.App.Application.Context;
public void StartNotificationsService()
{
//Start Notifications Service
Intent myIntent = new Intent(context, typeof(NotificationsService));
context.StartService(myIntent);
//Show Snackbar message
Activity activity = CrossCurrentActivity.Current.Activity;
Android.Views.View activityRootView = activity.FindViewById(Android.Resource.Id.Content);
Snackbar.Make(activityRootView, "Notifications activées", Snackbar.LengthLong).Show();
}
public void StopNotificationsService()
{
//Stop Notifications Service
Intent myIntent = new Intent(context, typeof(NotificationsService));
context.StopService(myIntent);
//Show Snackbar message
Activity activity = CrossCurrentActivity.Current.Activity;
Android.Views.View activityRootView = activity.FindViewById(Android.Resource.Id.Content);
Snackbar.Make(activityRootView, "Notifications désactivées", Snackbar.LengthLong).Show();
}
}
}
这是服务类别
namespace FleetDesigner.Droid
{
[Service
(Name = "com.fleet.app.NotificationsService",
Process = "com.fleet.app.NotificationsService",
Label = "Service Notifications"
)]
class NotificationsService : Service
{
AlarmManager manager;
Intent myIntent;
PendingIntent pendingIntent;
public override IBinder OnBind(Intent intent)
{
return null;
}
//[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
//return base.OnStartCommand(intent, flags, startId);
manager = (AlarmManager)GetSystemService(Context.AlarmService);
myIntent = new Intent(this, typeof(NotificationsAlarm));
pendingIntent = PendingIntent.GetBroadcast(this, 0, myIntent, 0);
manager.SetRepeating(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime() + 1000, 60 * 1000, pendingIntent);
return StartCommandResult.NotSticky;
}
public override void OnDestroy()
{
base.OnDestroy();
pendingIntent.Cancel();
manager.Cancel(pendingIntent);
}
}
}
最后,这是广播接收器(该服务的逻辑只是显示本地通知)
namespace FleetDesigner.Droid
{
[BroadcastReceiver(Enabled = true)]
public class NotificationsAlarm : BroadcastReceiver
{
static Random random = new Random();
public override void OnReceive(Context context, Intent intent)
{
//showNotifications(context, intent);
int m = random.Next(9999 - 1000) + 1000;
var title = "Title";
var channelName = "TestChannel";
string channelID = "TestChannel";
string OverSpeedChannelName = "OverSpeedChannel";
string notificationChannel = channelName;
using (var notificationManager = NotificationManager.FromContext(context))
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
NotificationChannel channel = null;
if (channel == null)
{
channel = new NotificationChannel(channelName, channelName, NotificationImportance.Default)
{
LockscreenVisibility = NotificationVisibility.Public
};
channel.SetShowBadge(true);
notificationManager.CreateNotificationChannel(channel);
}
channel.Dispose();
NotificationChannel overspeedchannel = null;
if (overspeedchannel == null)
{
overspeedchannel = new NotificationChannel(OverSpeedChannelName, OverSpeedChannelName, NotificationImportance.Default)
{
LockscreenVisibility = NotificationVisibility.Secret
};
overspeedchannel.SetShowBadge(true);
notificationManager.CreateNotificationChannel(overspeedchannel);
}
overspeedchannel.Dispose();
}
var notificationBuilder = new NotificationCompat.Builder(context, channelID)
.SetContentTitle("Title "+m)
.SetContentText("Notification Content")
.SetSmallIcon(Resource.Drawable.ic_action_map)
.SetShowWhen(false)
.SetChannelId(notificationChannel);
var notification = notificationBuilder.Build();
notificationManager.Notify(m, notification);
}
}
}
所以,正如我所说的,此代码中的唯一问题是..即使我调用stopService(),通知(后台服务)也不会停止!
有什么建议吗?谢谢