*在实际运行中同样的Service的确只能有一个。

Services有两种启动形式:

  • Started:其他组件调用startService()方法启动一个Service。一旦启动,Service将一直运行在后台(run in the background indefinitely)即便启动Service的组件已被destroy。通常,一个被start的Service会在后台执行单独的操作,也并不给启动它的组件返回结果。比如说,一个start的Service执行在后台下载或上传一个文件的操作,完成之后,Service应自己停止。

    • 开启方式 :startService()

      SwipeActivity.this.startService(new Intent(SwipeActivity.this, FindCardService.class));
    • 结束方式1:
      SwipeActivity.this.stopService(new Intent(SwipeActivity.this, FindCardService.class));
    • 结束方式2:
      stopSelf(); // 继承Service的子类内部调用
    • 生命周期:
      onCreate() --> onStartCommand() --> onDestroy()  // 如果Service已经启动,再次startService(),只会调用onStartCommand()方法,控件销毁时,记得解绑
  • Bound:其他组件调用bindService()方法绑定一个Service。通过绑定方式启动的Service是一个client-server结构,该Service可以与绑定它的组件进行交互。一个bound service仅在有组件与其绑定时才会运行(A bound service runs only as long as another application component is bound to it),多个组件可与一个service绑定,service不再与任何组件绑定时,该service会被destroy。

    • 开启方式:bindService()

      SwipeActivity.this.bindService(new Intent(SwipeActivity.this, FindCardService.class), connection, BIND_AUTO_CREATE);
    • 结束方式:unbindService()
      SwipeActivity.this.unbindService(connection);  // 解绑只能进行一次,不然会出现崩溃: Service not registered
    • 生命周期:
      onCreate() --> onBind() --> onDestroy();  // 如果Service已经启动,再次bindService(),没效果
    • 使用如下:
    private FindCardService.FindCardBinder mFindCardBinder;
private boolean mBound; // 防止调用unbindService崩溃
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
CoreLog.d(TAG, "ServiceConnection -- onServiceConnected()");
mFindCardBinder = (FindCardService.FindCardBinder) service;
mBound = true;
} // 只有在service因异常而断开连接的时候,这个方法才会用到
@Override
public void onServiceDisconnected(ComponentName name) {
CoreLog.d(TAG, "ServiceConnection -- onServiceDisconnected()");
mBound = false;
}
};

绑定:

findViewById(R.id.bangding).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SwipeActivity.this.bindService(new Intent(SwipeActivity.this, FindCardService.class), connection, BIND_AUTO_CREATE);
}
});

解绑:

 @Override
protected void onDestroy() {
super.onDestroy();
if (mBound) {
SwipeActivity.this.unbindService(connection);
}
}

Service:

    @Nullable
@Override
public IBinder onBind(Intent intent) {
CoreLog.d(TAG, "FindCardService -- onBind()");
return mFindCardBinder;
} private FindCardBinder mFindCardBinder = new FindCardBinder(); public class FindCardBinder extends Binder { public void startFindCard() { } }

额外情况:

如果使用了bindService和startService混合使用会是什么情况?

1.stopService()会失效,即使使用了stopService(),不会调用onDestory()方法。

其他的,会按照各自的生命周期来走。

2.当控件都解绑了,才会调用onDestory()方法。

3.在Service每一次的开启关闭过程中,只有onStart可被多次调用(通过多次startService调用),其他onCreate,onBind,onUnbind,onDestory在一个生命周期中只能被调用一次。也就是说Activity A 在onCreate()方法中bindService,正常调用onBind(),但没有在onDestory()方法中unbindService。Activity B在onCreate()方法中bindService,但没有调用onBind()方法了。

Services的更多相关文章

  1. Fedora 22中的Services and Daemons

    Introduction Maintaining security on your system is extremely important, and one approach for this t ...

  2. ABP(现代ASP.NET样板开发框架)系列之15、ABP应用层——应用服务(Application services)

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之15.ABP应用层——应用服务(Application services) ABP是“ASP.NET Boiler ...

  3. .NET RESTful Web Services入门

    很早之前看到过RESTful Web Services,并未在意,也没找相关资料进行学习.今天偶尔有一机会,就找了点资料进行研究,发现RESTful真是“简约而不简单”.下面用示例来说明: 1 项目结 ...

  4. 分分钟带你玩转 Web Services

    当大型需求被数个公司分割开来,各公司系统相互交换数据的问题就会接踵而来. 毕竟是多家不同的公司的产品,研发开发语言.采用技术框架基本上是百花齐放. 怎样让自家系统提供的服务具有跨平台.跨语言.跨各种防 ...

  5. 如何用Azure Web App Services接入微信公众号

    注:本文提到的代码示例下载地址>如何用Azure Web App Services接入微信公众号 如何用Azure Web App Services接入微信公众号 简介 此示例演示如何创建Azu ...

  6. 用C#创建Windows服务(Windows Services)

    用C#创建Windows服务(Windows Services) 学习:  第一步:创建服务框架 创建一个新的 Windows 服务项目,可以从Visual C# 工程中选取 Windows 服务(W ...

  7. Windows HTTP Services

    原文:https://msdn.microsoft.com/zh-cn/library/windows/desktop/aa384273(v=vs.85).aspx Purpose (目的) Micr ...

  8. 单例模式和angular的services的使用方法

    在现实生活中存在着有这样的特点的一些类: A.这些类只能有一个实例: B.这些能够自动实例化: C.这个类对整个系统可见,即必须向整个系统提供这个实例. 不妨举一个具体的单例模式的例子:比如教室里面的 ...

  9. Apache许可协议Open RIA Services

    Jeff Handley's进行了多年的项目--基于一份开源许可发布WCF RIA Services.遵循Apache 2许可,捐赠给Outercurve基金会的ASP.NET Open Source ...

  10. 当程序以Windows Services形式启动时当前路径不对

    当程序以Windows Services形式启动时当前路径不对 @(操作系统)[博客|dotNet] 很多时候我们需要将我们的程序写成利用Windows服务的形式来让它能够自启动.今天遇到一个问题,当 ...

随机推荐

  1. 二进制转16进制JAVA代码

    public class Binary2Hex { public static void main(String[] args) { String bString ="10101000&qu ...

  2. webpack 小记

    零.入口与输出 //对像语法 entry: { aa: __dirname + '/src/aa.js',    //(chunkName :path) bb: __dirname + '/src/b ...

  3. HDFS的xshell及dfsadmin命令

    一. DFS:distributied file system 是一种允许文件通过网络在多台主机上风向的文件系统,可让多机器上的多用户分享文件和存储空间 二.HDFS的shell **切记后面加的 / ...

  4. loj6029 「雅礼集训 2017 Day1」市场

    传送门:https://loj.ac/problem/6029 [题解] 考虑如果有一些近似连续的段 比如 2 2 2 3 3 3,考虑在除3意义下,变成0 0 0 1 1 1,相当于整体-2 又:区 ...

  5. 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)

    Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...

  6. [bzoj1823][JSOI2010]满汉全席——2-SAT

    题目大意 题目又丑又长我就不贴了,说一下大意,有n种菜,m个评委,每一个评委又有两种喜好,每种菜有满汉两种做法,只能选一种.判断是否存在一种方案使得所有评委至少喜欢一种菜品.输入包含多组数据. 题解 ...

  7. 详解SHOW PROCESSLIST显示哪些线程正在运行列出的状态

    SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPER权限,您可以看到所有线程.否则,您只能看到您自己的线程( ...

  8. inside a shard

    fsync sync fsync/syncsync is a standard system call in the Unix operating system, which commits to d ...

  9. Selenium2+python自动化70-unittest之跳过用例(skip)【转载】

    前言 当测试用例写完后,有些模块有改动时候,会影响到部分用例的执行,这个时候我们希望暂时跳过这些用例. 或者前面某个功能运行失败了,后面的几个用例是依赖于这个功能的用例,如果第一步就失败了,后面的用例 ...

  10. hdu 1054(最小点覆盖集)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...