一、服务的生命周期

服务与活动一样,在它的整个生命周期中存在着一些事件,下图可以很好解释整个过程以及涉及到的方法:

在真实的使用中,Service来还包含一个OnBind方法,并且必须要使用该方法,但是只要返回NULL即可,除非当前服务是一个绑定服务,那么就要返回实现了IBinder的实例。

二、回调方法的总结

上图中涉及到了几个方法,下面将做简单的介绍:

OnCreate:只会在服务第一次开启的时候调用,主要负责一些初始化代码

OnStartCommand:每次启动服务都会调用该方法,可能来自StartService或者由系统重启。一般负责开启需要长时间的任务。并且该方法还要返回StartCommandResult类型的枚举,该返回值将影响系统重启该服务的细节。

OnDestroy:当服务使用StopSelf或者StopService时调用,主要用来释放资源等。

三、返回不同StartCommandResult服务的区别

Sticky:当服务由于内存不够被系统关闭后,将会由系统重启该服务,但是传入OnStartCommand方法的intent参数为NULL,意味着该类型的服务无法恢复上次的状态,只能进行常规的长时间任务。

RedeliverIntent:该类型的服务与Sticky的唯一的区别就是在系统重启该服务时,将会将上次关闭的服务的状态传递给OnStartCommand方法,用来恢复上次的任务继续执行。适合需要长时间连续的任务。

NotSticky:该服务被系统关闭后将不会重启。

StickyCompatibility:在API 5或以上的环境中的行为与Sticky一样,相反在API 5以下可能不会重启服务。

四、实现一个服务

这里我们需要继承自Service并还要需要加上Service注解属性(项目自行新建

 namespace ServiceStudy
{
[Service]
public class MainService : Service
{ }
}

其中[Service]负责在AndroidManifest.xml注册服务,比如上面的服务将会生成如下字符串:

 <service android:name="ServiceStudy.MainService"></service>

下面我们开始具体实现一个服务,上面已经说明了无论任何服务都要重写OnBind方法,所以我们先重写该方法:

         public override Android.OS.IBinder OnBind(Android.Content.Intent intent)
{
return null;
}

然后是OnCreate方法:

         public override void OnCreate()
{
base.OnCreate();
Log.Debug("xamarin", "创建服务");
}

接着是OnStartCommand方法:

         public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
{
Log.Debug("xamarin", "启动服务");
return StartCommandResult.Sticky;
}

最后就是OnDestroy方法:

         public override void OnDestroy()
{
base.OnDestroy();
Log.Debug("xamarin", "关闭服务");
}

这样一个简单的服务就完成了,下面是整体代码:

     [Service]
public class MainService : Service
{
public override void OnCreate()
{
base.OnCreate();
Log.Debug("xamarin", "创建服务");
} public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
{
Log.Debug("xamarin", "启动服务");
return StartCommandResult.Sticky;
} public override void OnDestroy()
{
base.OnDestroy();
Log.Debug("xamarin", "关闭服务");
} public override Android.OS.IBinder OnBind(Android.Content.Intent intent)
{
return null;
}
}

五、启用与停止服务

有了上面的服务我们现在就可以开启它了,开启服务的方法如下:

 StartService(new Intent(this, typeof(MainService)));

停止服务的方法如下:

 StopService(new Intent(this, typeof(MainService)));

首先打开Main.axml,再拖拽一个按钮进去,并设置他们的Text,以及id为@+id/startButton@+id/stopButton,结果如下:

接着打开MainActivity.cs文件,为这两个按钮绑定监听事件:

     public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button startButton = FindViewById<Button>(Resource.Id.startButton);
Button stopButton = FindViewById<Button>(Resource.Id.stopButton); startButton.Click += delegate
{
StartService(new Intent(this, typeof(MainService)));
}; stopButton.Click += delegate
{
StopService(new Intent(this, typeof(MainService)));
};
}
}

最终在模拟机上进行测试,得出下面的结果:

从中可以看到多个连续的启动服务,因为笔者按下了返回退出了应用,然后再返回到应用中,开启服务,那么就只显示启动服务了,而不会经过创建服务了。

六、使用startId关闭服务

通过上面的例子,大家一定有人不停的开启服务。当然每次开启的都是一个新的服务。但是关闭服务的时候并不是关闭其中一个,而是把所有的服务都关闭了。由这个就需要考虑一种情况,就是如何区分不同的实例以及关闭不同的实例呢?

大家可以看看

服务中已经提供了专门的方法,当然如果是关闭当前的服务可以直接用StopSelf(),下面我们将OnStartCommand中重写,开启一个线程:

         public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
{
Log.Debug("xamarin", "启动服务");
new Thread(() =>
{
Log.Debug("xamarin", startId + "号服务的线程启动");
Thread.Sleep();
Log.Debug("xamarin", startId + "号服务的线程关闭");
StopSelf(startId);
}).Start();
return StartCommandResult.Sticky;
}

我们这里使用了StopSelft的重载版本关闭了服务。

然后我们再开始进行调试,首先我们按下一次开启服务,将出现如下的结果:

接着我们快速的点击两次开启服务,将出现如下的结果:

通过这张图我们可以看到,输出了1号和2号,同时在完成执行后,我们再点关闭服务就没有任何反应了,因为服务自己已经把自己关闭了。

七、通过Intent Filter开启服务

上面所有的服务开启方法都是通过类型开启的,但是这样的缺点显而易见,如果我们改变了服务的名称就需要改正其他的地方,而通过这节我们将可以使用字符串名称来开启服务。

这里我们需要使用IntentFilter注解属性,比如下面这样的注解属性:

则会在AndroidManifest.xml中生成如下的字符串:

 <service android:name="ServiceStudy. MainService">
<intent-filter>
<action android:name="xamarin-cn.com.mainservice" />
</intent-filter>
</service>

我们先给MainService加上Intent Filter

     [Service]
[IntentFilter(new string[]{"xamarin-cn.com.mainservice"})]
public class MainService : Service

然后修改开启服务地方的代码:

         protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button startButton = FindViewById<Button>(Resource.Id.startButton);
Button stopButton = FindViewById<Button>(Resource.Id.stopButton); startButton.Click += delegate
{
StartService(new Intent("xamarin-cn.com.mainservice"));
}; stopButton.Click += delegate
{
StopService(new Intent("xamarin-cn.com.mainservice"));
};
}

这里我们依然还是使用Intent但是传递的参数已经变成了字符串。

Xamarin.Android服务的实现的更多相关文章

  1. [译]:Xamarin.Android平台功能——位置服务

    返回索引目录 原文链接:Location Services. 译文链接:Xamarin.Android平台功能--位置服务 本部分介绍位置服务以及与如何使用位置提供商服务 Location Servi ...

  2. Xamarin.Android其他类型的服务

    一.前言 前面我们已经学了关于服务的很多知识,但是对于真实的开发那些远远不够,通过这节我们将学习其他类型的服务,比如前台服务.IntentService和消息服务.下面我们开始进入正题. 二.前台服务 ...

  3. XAMARIN.ANDROID SIGNALR 实时消息接收发送示例

    SignalR 是一个开发实时 Web 应用的 .NET 类库,使用 SignalR 可以很容易的构建基于 ASP.NET 的实时 Web 应用.SignalR 支持多种服务器和客户端,可以 Host ...

  4. APP并非一个人在战斗,还有API—Xamarin.Android回忆录

    前言 一般来说,一个客户端APP并非独立存在的,很多时候需要与服务器交互.大体可分为两方面的数据,常规字符串数据和文件数据,因为这两种数据很可能传输方式不一样,比如字符串之类的数据,使用HTTP协议, ...

  5. Xamarin.Android通知详解

    一.发送通知的机制 在日常的app应用中经常需要使用通知,因为服务.广播后台活动如果有事件需要通知用户,则需要通过通知栏显示,而在Xamarin.Android下的通知需要获取Notification ...

  6. [译]:Xamarin.Android开发入门——Hello,Android Multiscreen深入理解

    原文链接:Hello, Android Multiscreen_DeepDive. 译文链接:Xamarin.Android开发入门--Hello,Android Multiscreen深入理解. 本 ...

  7. Xamarin.Android之封装个简单的网络请求类

    一.前言 回忆到上篇 <Xamarin.Android再体验之简单的登录Demo> 做登录时,用的是GET的请求,还用的是同步, 于是现在将其简单的改写,做了个简单的封装,包含基于Http ...

  8. Xamarin.Android之MvvmCross

    欢迎大家加入以下开源社区 Xamarin-Cn:https://github.com/Xamarin-Cn Mvvmcross-Cn:https://github.com/Mvvmcross-Cn  ...

  9. Xamarin.Android开发实践(十七)

    Xamarin.Android之定位 一.前言 打开我们手中的应用,可以发现越来越多的应用使用了定位,从而使我们的生活更加方便,所以本章我们将学习如何在Xamarin中进行定位的开发. 二.准备工作 ...

随机推荐

  1. SRE学习笔记:分布式共识系统、Paxos协议

    最近阅读了<SRE Google运维解密>的第23章,有一些感触,记录一下. 日常工作中,我们经常需要一些服务分布式的运行.跨区域如跨城.跨洲部署运行分布式系统往往是容易的,但是如何保证各 ...

  2. 部署Percona XtraDB Cluster高可用和多Master集群

    http://www.it165.net/admin/html/201401/2306.html http://www.oschina.net/p/percona-xtradb-cluster/ ht ...

  3. Django创建自定义错误页面400/403/404/500等

    直接参考: https://zhuanlan.zhihu.com/p/38006919 DEBUG =True的话,为开发环境,显示不了404页面.

  4. IE9版本以下ajax 跨域问题解决

    ajax跨域请求数据在谷歌火狐我本地IE11都是没问题的. 让测试就发现问题了,IE8下请求不到数据,然后我查看一下自己写的js看有没有不兼容问题,可是都没有啊,为什么就请求不到呢. 我把ajax的e ...

  5. 给Spring的placeholder设置默认值

    问题:使用Spring时,可以方便地通过placeholder的形式${key}将key对应的properities定义value,注入到Bean中.但是如果在properities文件中,没有对ke ...

  6. 关于微软C#中的CHART图表控件的简单使用【转】

    最近公司项目要用到Chart图表控件,这是一个比较老的东西了,目前网络上似乎已经不太流行这个控件,但是只要配置了相关的属性,效果还是可以的.前前后后摸索了好久,接下来谈谈这个件控件最重要的几个属性. ...

  7. easyui combobox默认选中项

    今天写前端代码发现combobox还挺难搞, $("#select_Dic").combobox({                        url: "http: ...

  8. 事件分发机制 事件拦截 滑动冲突 MD

    目录 事件分发机制分析案例 默认行为 试验 0 结论 dispatchTouchEvent 返回 true 试验 1 试验 2 结论 onInterceptTouchEvent 返回 true 试验 ...

  9. AOP AspectJ 字节码 语法 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  10. js改变iframe 的src地址

    <script> function dizhi(){ document.getElementById("aaa").src='http://www.sohu.com' ...