Xamarin Android 8.0 创建前景服务的个人粗略总结(不是详细教程)

Xamarin Android 8.0 创建前景服务的个人粗略总结(不是详细教程),第1张

概述我说的可能有错误主要的是要想使前景服务不会睡眠,则需要定时更新通知,譬如下载服务更新进度,否则我的测试是一会就不运行了,类似挂起而不是杀死 1.前景服务需要创建通知,面向8.0需要事先创建通知管道2.8.0启动前景服务需要使用StartForegroundService而不是StartServer3.在

我说的可能有错误

主要的是要想使前景服务不会睡眠,则需要定时更新通知,譬如下载服务更新进度,否则我的测试是一会就不运行了,类似挂起而不是杀死

 

1.前景服务需要创建通知,面向8.0需要事先创建通知管道

2.8.0启动前景服务需要使用StartForegroundService而不是StartServer

3.在服务中需要调用StartForeground传入一个通知对象将自己注册为前景服务

4.要使前景服务不会睡眠,则需要定时更新通知

 

创建最简单最粗略的通知管道的方法,主要的参数是管道ID,创建通知时要使用

public static voID CreateNotificationChannel(Contextwrapper context, string channelID, string channelname)        {            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)            {                ((notificationmanager)context.GetSystemService(Context.NotificationService))                            .CreateNotificationChannel(new NotificationChannel(channelID, channelname, Notificationimportance.Default));            }        }

 

下面是创建通知的简化方法, 一个通知标题,文本,图标,一个前景服务貌似必须设置为True的项, 通知管道ID在NotificationCompat.Builder构造函数填入

 

public static Notification CreateServerNotification(string contentTitle, string contentText, Context context, string channelID)        {            return new NotificationCompat.Builder(context, channelID)                               .SetContentTitle(contentTitle)                               .SetContentText(contentText)                               .SetSmallicon(Resource.Mipmap.icon)                               .Setongoing(true)                               .Build();        }

 

发布通知的方法,更新通知只需要通知ID(不是管道ID)不变重新创建一个Notification对象然后调用便可,

        public static voID CreateUpServerNotificationFunc(Contextwrapper context, int notificationID, Notification notification)        {            ((notificationmanager)context.GetSystemService(Context.NotificationService))                           .Notify(notificationID, notification);        }

 

 

然后androID 8.0开启前景服务需要使用StartForegroundService

 public static voID StartServer(Contextwrapper context, Intent intent)        {            context.StartForegroundService(intent);        }

然后服务可以这样写

    [Service]    public sealed class MyServer : Service    {                public overrIDe voID OnCreate()        {            base.OnCreate();                        const int NOTIFICATION_ID = 345;            const string CHANNEL_ID = "自己起一个ID";            const string CHANNEL_name = "自己起一个name";            CreateNotificationChannel(this, CHANNEL_ID, CHANNEL_name);            var notification = CreateServerNotification("标题", "内容", this, CHANNEL_ID);            StartForeground(NOTIFICATION_ID, notification);        }        [return: GeneratedEnum]        public overrIDe StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startID)        {            return StartCommandResult.NotSticky;        }              public overrIDe voID OnDestroy()        {            base.OnDestroy();        }        public overrIDe IBinder OnBind(Intent intent)        {            return null;        }    }

 

总结

以上是内存溢出为你收集整理的Xamarin Android 8.0 创建前景服务的个人粗略总结(不是详细教程)全部内容,希望文章能够帮你解决Xamarin Android 8.0 创建前景服务的个人粗略总结(不是详细教程)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1041201.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存