xamarin android 如何创建一个服务
16lz
2021-01-23
//规定红字处一定要小写 以下这个放在Properties/AdnroidManifest.xml文件中 intelligenceLamp.WebSocketService" android:enabled="true" android:process=":remote">
以下这个自己创建一个.cs文件即可 创建的服务: using System; using System.Collections.Generic; using System.Linq; using System.Text;
using Android.App; using Android.Content; using Android.Content.Res; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget;
namespace IntelligenceLamp { [Service] class WebSocketService : Service { public override IBinder OnBind(Intent intent) { throw new NotImplementedException(); } public override void OnCreate() { base.OnCreate(); Console.WriteLine("服务被创建"); }
[return: GeneratedEnum] public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId) { Console.WriteLine("服务被启动"); return base.OnStartCommand(intent, flags, startId); } public override void OnDestroy() { Console.WriteLine("服务被关闭"); base.OnDestroy(); } } }
放在MainActivity.cs 的oncreat函数中就可以了(即初始化函数) 开启服务: StartService(new Intent(this, typeof(WebSocketService)));
以下这个自己创建一个.cs文件即可 创建的服务: using System; using System.Collections.Generic; using System.Linq; using System.Text;
using Android.App; using Android.Content; using Android.Content.Res; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget;
namespace IntelligenceLamp { [Service] class WebSocketService : Service { public override IBinder OnBind(Intent intent) { throw new NotImplementedException(); } public override void OnCreate() { base.OnCreate(); Console.WriteLine("服务被创建"); }
[return: GeneratedEnum] public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId) { Console.WriteLine("服务被启动"); return base.OnStartCommand(intent, flags, startId); } public override void OnDestroy() { Console.WriteLine("服务被关闭"); base.OnDestroy(); } } }
放在MainActivity.cs 的oncreat函数中就可以了(即初始化函数) 开启服务: StartService(new Intent(this, typeof(WebSocketService)));
更多相关文章
- android--------根据文件路径使用File类获取文件相关信息
- Android Audio代码分析13 - AudioTrack::getPosition函数
- 安卓4.X系统 增加蓝牙接收文件类型
- 用SAX解析xml文件
- NFS挂载android文件系统
- android 9.0 SD卡权限问题 文件管理器没有权限
- android 读取文件内容操作