import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class BindService extends Service{
  private int count;
  private boolean quit;
  //定义onBinder方法所返回的对象
  private MyBinder binder = new MyBinder();
  //通过继承Binder来实现IBinder类
  public class MyBinder extends Binder{
    public int getCount(){
      //获取Service的运行状态:count
      return count;
    }
  }

  //必须实现的方法
  @Override
  public IBinder onBind(Intent intent) {
    System.out.println("Service is Binded");
    //返回IBinder对象
    return binder;
  }

  //Service被创建时回调该方法
  @Override
  public void onCreate() {
    super.onCreate();
    System.out.println("Service is Created");
    //启动一条线程、动态地修改count状态值
    new Thread(){
      public void run() {
        while(!quit){
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          count++;
        }
      }
    }.start();
  }
  //Service被断开连接时回调该方法
  @Override
  public boolean onUnbind(Intent intent) {
    System.out.println("Service is Unbinded");
    return true;
  }

  //Service被关闭之前回调
  @Override
  public void onDestroy() {
    super.onDestroy();
    this.quit = true;
    System.out.println("Service is Destroyed");
  }

}

绑定本地Service并与之通信-----之一的更多相关文章

  1. Android菜鸟的成长笔记(18)——绑定本地Service并与之通信

    在上一篇中介绍了Service与Activity的区别及Service两种启动方式中的第一种启动方式startService(). 我们会发现用startService().stopService() ...

  2. 绑定本地Service并与之通信-----之二

    import android.os.Bundle;import android.os.IBinder;import android.app.Activity;import android.app.Se ...

  3. 绑定本地Service并与之通信

    绑定Service需要调用 public boolean bindService (Intent service, ServiceConnection conn, int flags): 传入一个Se ...

  4. Android开发学习之路-Service和Activity的通信

    在很多时候,Service都不仅仅需要在后台运行,还需要和Activity进行通信,或者接受Activity的指挥,如何来实现,来看代码. 定义一个服务 // 创建一个服务,然后在onBind()中返 ...

  5. Android Service、IntentService,Service和组件间通信

    Service组件 Service 和Activity 一样同为Android 的四大组件之一,并且他们都有各自的生命周期,要想掌握Service 的用法,那就要了解Service 的生命周期有哪些方 ...

  6. 绑定本地的Session

    绑定本地的Session图示解析: 代码的结构: 代码: SaveServlet.java package com.itheima.servlet; import java.io.IOExceptio ...

  7. C# 开源一个基于 yarp 的 API 网关 Demo,支持绑定 Kubernetes Service

    关于 Neting 刚开始的时候是打算使用微软官方的 Yarp 库,实现一个 API 网关,后面发现坑比较多,弄起来比较麻烦,就放弃了.目前写完了查看 Kubernetes Service 信息.创建 ...

  8. 【Service Fabric】小白入门记录 本地Service Fabric集群安装及设置

    本篇内容是自学自记,现在我还不知道Service Fabric究竟是怎么个入门法,反正按照入门教程先进行本地Service Fabric集群的安装,万里路始于足下,要学习总得先把环境装好了才能开始学习 ...

  9. socket的bind函数是不是只能绑定本地IP,不能绑定外网IP么?

    参考: https://bbs.csdn.net/topics/391024376 别瞎猜测. 所谓bind,就是指绑定本地接受端口. 指定ip,是为了分辨多ip主机. --------------- ...

随机推荐

  1. iOS - UIRefreshControl 刷新数据

    前言 NS_CLASS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED @interface UIRefreshControl : UIControl 1.UIRefresh ...

  2. Class.asSubclass浅谈

    这是java.lang.Class中的一个方法,作用是将调用这个方法的class对象转换成由clazz参数所表示的class对象的某个子类.举例来说, 上面的代码将strList.getClass() ...

  3. nodejs学习笔记<二>简单的node服务器

    在环境搭建好后,就可以开始动手架设(node驱动)一个简单的web服务器. 首先,nodejs还是用js编写.先来看一段node官网上的实例代码. var http = require('http') ...

  4. boost线程的问题:

    可以看看这里:http://blog.csdn.net/misskissc/article/details/9859753 我的总结: 1,用thread类来创建一个线程,它的构结函数 : (1)th ...

  5. MyBatis——优化MyBatis配置文件中的配置

    原文:http://www.cnblogs.com/xdp-gacl/p/4264301.html 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写 ...

  6. hiho_1051_补提交卡

    题目大意 给出1到100这100个数中的某些数字(各个数字不同),这些数字形成一个个间断的连续区间,向1-100中添加M个数字,使得添加后1-100中某连续区间的长度最大,求出添加M个数字后,最长的连 ...

  7. hdu_2089 不要62

    数位动态规划     数位动态规划是求解一个大区间[L, R]中间满足条件Q的所有数字的个数(或者和,或其他)的一种方法.它通过分析每一位上的数字,一般用 dp[len][digit][...] 来表 ...

  8. listToString

    http://www.oschina.net/code/snippet_109648_2229

  9. Python核心编程-基础2

    open() 和 file() 函数会同时存在, 完成相同的功能.一般说来, 我们建议使用 open() 来读写文件, 在您想说明您在处理文件对象时使用 file() , 例如 if instance ...

  10. 如何连接git bash和git hub

    git config --global user.name "Your Real Name" 2 git config --global user.email you@email. ...